0

I get a JSON object from an AJAX call that I need to get all the "name" values from.

I have tried to iterate it in many different ways without success. I have managed to iterate JSON objects before but now the values are inside pkg which is inside data: and that is where i get trouble. code below does not work and I do not know how to get into pkg.

for (var key in jsonData) {
    if (jsonData.hasOwnProperty(key)) {

    }
    jQuery('#result').html(jsonData[key].join("<br/>"));
}

No success with (jsonData.pkg) or (jsonData.data.pkg) either.

Underneath is the value inside jsonData object.

{
    "data": {
        "pkg": [{
            "name": "ThisIsName1",
            "FRONTPAGE": "n",
            "IP": "n",
            "MAXSQL": "1",
            "MAXPOP": "unlimited"
        }, {
            "name": "ThisIsName2",
            "FRONTPAGE": "n",
            "IP": "n",
            "MAXSQL": "0",
            "MAXPOP": "unlimited"
        }, {
            "name": "ThisIsName3",
            "FRONTPAGE": "n",
            "IP": "n",
            "MAXSQL": "0",
            "MAXPOP": "unlimited"
        }]
    },
    "metadata": {
        "version": 1,
        "reason": "OK",
        "result": 1,
        "command": "listpkgs"
    }
}

I have searched the forum for answers but I have not found someone that has the values nested inside the object.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
conan
  • 35
  • 9
  • you can look **[here](http://stackoverflow.com/questions/1078118/how-do-i-iterate-over-a-json-structure)** – The Reason Jun 24 '15 at 08:06
  • @herriekrekel I have done -> var jsonData = JSON.stringify(data); – conan Jun 24 '15 at 08:07
  • You have objects in an array (pkg) in an object (data) in an object (jsonData). If you adress `jsonData.data.pkg[0].name` you get *ThisISName1*. `jsonData[key]` will only return an object. [here is a fiddle](https://jsfiddle.net/Lkd5gp51/) – Michel Jun 24 '15 at 08:11
  • Have a look at this, all the other answers are unnecessarily confusing. http://jsfiddle.net/xmq81s7u/ – Rob Schmuecker Jun 24 '15 at 08:11
  • @conan JSON.stringify turns an object in to a JSON text and stores that JSON text in a string. JSON.parse turns a string of JSON text into an object. if you want to use any of the answers here go with the parse see what happens – herriekrekel Jun 24 '15 at 08:17

5 Answers5

2

Try this code. Ensure you have parsed the json string. And use array map function to get all names.

var jsonString = '{"data":{"pkg":[{"name":"ThisIsName1","FRONTPAGE":"n","IP":"n","MAXSQL":"1","MAXPOP":"unlimited"},{"name":"ThisIsName2","FRONTPAGE":"n","IP":"n","MAXSQL":"0","MAXPOP":"unlimited"},{"name":"ThisIsName3","FRONTPAGE":"n","IP":"n","MAXSQL":"0","MAXPOP":"unlimited"}]},"metadata":{"version":1,"reason":"OK","result":1,"command":"listpkgs"}}';

var jsonData = JSON.parse(jsonString);
var names = jsonData.data.pkg.map(function(pkg){ return pkg.name }).join("<br/>");
jQuery('#result').html(names);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"></div>
Gilsha
  • 14,431
  • 3
  • 32
  • 47
0
    for(i=0;i<jsonObj.data.pkg.length;i++){

alert(jsonObj.data.pkg[i].name;


}
0
Array.prototype.map.call( jsonData.data.pkg ,function( el,i ) {

/*
Do with el whatever you want, here it will be object like -> {"name":"ThisIsName1","FRONTPAGE":"n","IP":"n","MAXSQL":"1","MAXPOP":"unlimited"}
*/

});
guramidev
  • 2,228
  • 1
  • 15
  • 19
0

You can try one of 3 options

var myObject = {"data":
{
    "pkg":[{"name":"ThisIsName1","FRONTPAGE":"n","IP":"n","MAXSQL":"1","MAXPOP":"unlimited"},{"name":"ThisIsName2","FRONTPAGE":"n","IP":"n","MAXSQL":"0","MAXPOP":"unlimited"},{"name":"ThisIsName3","FRONTPAGE":"n","IP":"n","MAXSQL":"0","MAXPOP":"unlimited"}]},
    "metadata":{"version":1,"reason":"OK","result":1,"command":"listpkgs"}
}

console.log("option a:");
for (a of myObject.data.pkg) console.log(a.name);

console.log("option b:");
for (a in myObject.data.pkg) console.log(myObject.data.pkg[a].name);

console.log("option c:");
myObject.data.pkg.forEach(function (element) { console.log(element.name); });
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
0

I offer this function if you want to iterate over a JSON object and get all its keys.

Please take in account it could not work in old browsers...

    var json = {"data":
{"pkg":[{"name":"ThisIsName1","FRONTPAGE":"n","IP":"n","MAXSQL":"1","MAXPOP":"unlimited"},
{"name":"ThisIsName2","FRONTPAGE":"n","IP":"n","MAXSQL":"0","MAXPOP":"unlimited"},
{"name":"ThisIsName3","FRONTPAGE":"n","IP":"n","MAXSQL":"0","MAXPOP":"unlimited"}]},
"metadata":{"version":1,"reason":"OK","result":1,"command":"listpkgs"}};


function getAllChildren(json, root) {

    function getChildren(json) {
        var result = [];
        for (var key in json) {
            result.push(key);
        }
        return result;
    }

    function isObject(json) {
        return typeof json == "object";
    }

    root = root || "";
    if (root.length > 0) {
        root += ".";
    }
    var result = [];
    var childs = getChildren(json);
    for (var i = 0; i < childs.length; i++) {
        result.push(root + childs[i]);
        if (isObject(json[childs[i]])) {
            var subchilds = getAllChildren(json[childs[i]], root + childs[i]);
            for (var j = 0; j < subchilds.length; j++) {
                result.push(subchilds[j]);
            }            
        }
    }

    return result;
}

getAllChildren(json);

And it returns:

["data",
"data.pkg",
"data.pkg.0",
"data.pkg.0.name",
"data.pkg.0.FRONTPAGE",
"data.pkg.0.IP",
"data.pkg.0.MAXSQL",
"data.pkg.0.MAXPOP",
"data.pkg.1",
"data.pkg.1.name",
"data.pkg.1.FRONTPAGE",
"data.pkg.1.IP",
"data.pkg.1.MAXSQL",
"data.pkg.1.MAXPOP",
"data.pkg.2",
"data.pkg.2.name",
"data.pkg.2.FRONTPAGE",
"data.pkg.2.IP",
"data.pkg.2.MAXSQL",
"data.pkg.2.MAXPOP",
"metadata",
"metadata.version",
"metadata.reason",
"metadata.result",
"metadata.command"]
Jorgeblom
  • 3,330
  • 1
  • 23
  • 24