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.

5
  • you can look here Commented Jun 24, 2015 at 8:06
  • @herriekrekel I have done -> var jsonData = JSON.stringify(data); Commented Jun 24, 2015 at 8: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 Commented Jun 24, 2015 at 8:11
  • Have a look at this, all the other answers are unnecessarily confusing. jsfiddle.net/xmq81s7u Commented Jun 24, 2015 at 8: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 Commented Jun 24, 2015 at 8:17

5 Answers 5

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Your code works aswell, I appreciate all your help!
0
    for(i=0;i<jsonObj.data.pkg.length;i++){

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


}

2 Comments

@DonCorleone Thanks! I managed to get it to work with your code!
Glad you found it usefull ^^
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"}
*/

});

Comments

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); });

Comments

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.