TB.M
TB.M

Reputation: 363

Get data from complex Json structure using NodeJs

json Structure:

    {
    "id" : "1",
    "Data" : [
    {
            "name" : "abc",

        },
        {
            "name" : "option1",
            "position" : [
                {
                    "name" : "option1",
                    "status" : [
                        {
                            "code" : "0",
                            "value" : "OFF"
                        },
                        {
                            "code" : "1",
                            "value" : "ON"
                        }
                    ]
                }]
        }   ]   
}

Here,I want to get the data from above complex Json structure.How to do that, Have tried below code but giving error like; error: uncaughtException: Cannot read property 'status' of undefined

function myfunc(req,res){
var collectionname  = db.collection("col1");
collectionname.find({}).each(function(err, doc) {
   if(doc != null)
    {
        var fdata = [];
        for(var i =0;i<doc.Data.length;i++){
            fdata.push(doc.Data[i].position.status);
            }
        console.log("fdata............",fdata);
    }
});
}

Please help with the same.

Upvotes: 0

Views: 244

Answers (3)

Liberateur
Liberateur

Reputation: 1487

You can use foreach for prevent length undefined.

function myfunc(req,res)
{
    let collectionname = db.collection("col1");
    collectionname.find({}).each(function(err, doc)
    {
        if(doc != null)
        {
            let fdata = [];

            for(let i in doc.Data)
            {
                for(let j in doc.Data[i].position)
                {
                    fdata.push(doc.Data[i].position[j].status);
                }
            }

            console.log("fdata............", fdata);
        }
    });
}

@MikaelLennholm have right, for(let i in doc.Data) works but not recommanded, be careful not to use it in prototyped or object-built arrays.


EDIT:

function myfunc(req,res)
{
    db.collection('col1').find({}).each(function(err, doc)
    {
        if(err)
        {
            console.log('[INFOS] Request fail, more details:\n', err);
        }
        else if(doc)
        {
            let fdata = [];

            if(doc.Data && doc.Data.length)
            {
                for(let i = doc.Data.length-1; i >= 0; i--)
                {
                    if(doc.Data[i].position && doc.Data[i].position.length)
                    {
                        for(let j = doc.Data[i].position.length-1; j >= 0; j--)
                        {
                            if(doc.Data[i].position[j].status)
                            {
                                fdata = fdata.concat(doc.Data[i].position[j].status);
                            }
                        }
                    }
                }
            }

            console.log("[INFOS] Datas:\n", fdata);
        }
    });
}

Upvotes: 1

Thiện Sinh
Thiện Sinh

Reputation: 559

im a newbie at nodejs, hope this's correctly

//I assume this is a object or you can convert from string to object
var data = {
    "id": "1",
    "Data": [
        {
            "name": "option1",
            "position": [
                {
                    "name": "option1",
                    "status": [
                        {
                            "code": "0",
                            "value": "OFF"
                        },
                        {
                            "code": "1",
                            "value": "ON"
                        }
                    ]
                }]
        }]
}

var statusArr = data.Data[0].position[0].status;
console.log(...statusArr);

Result: { code: '0', value: 'OFF' } { code: '1', value: 'ON' }

Upvotes: 1

Anusha kurra
Anusha kurra

Reputation: 480

Parse through position array as well

function myfunc(req,res){
var collectionname  = db.collection("col1");
collectionname.find({}).each(function(err, doc) {
   if(doc != null)
    {
        var fdata = [];

        for(var i =0;i<doc.Data.length;i++){
        for(var j =0;j<doc.Data[i].position.length;j++){
            fdata.push(doc.Data[i].position[j].status);
            }
        }
        console.log("fdata............",fdata);
    }

}); }

Upvotes: 0

Related Questions