0

I am trying to print out the key and value names from the data below e.g. "Player 1' - 'ball' 'hat' and "Player 2 - 'ball, 'hat'. So keys have multiple value names but im not sure how i print these out.** I am getting '[object Object]' as a response**. Can someone help me with this understanding please and trying to resolve the matter.

Data

{
    "Player1": {
        "ball": 1,
        "hat": 2

    },
    "Player2": {
        "ball": 1,
        "hat": 2,
    }
}

JavaScript

$.getJSON('data.json', function (data) {
    var response = data;
    for (key in response){
        alert("key: " + key + "value :" + response[key]);
    }
});
1
  • Try using console.log("key:", key, "value:", response[key]); it'll allow you to see the objects, regardless of whether you stringify them. Commented Feb 21, 2015 at 19:19

5 Answers 5

6

The simplest way to do this in any modern browser would be to use Object.keys() and just join the result into a string, like this:

for (key in response){
    alert("key: " + key + " value :" + Object.keys(response[key]).join(' '));
}

Result:

key: Player1 value :ball hat
key: Player2 value :ball hat

You can test it out here.

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

Comments

4

Use JSON.stringify(response[key]) when printing the object.

2 Comments

But i only want the property names of the value, not the values too e.g. just "ball", "hat" and not : { ball: 1} etc
@user2219097 response[key] is also an object, so you can use a for each loop (for (var k in response[key])) to loop through and prints its keys.
1
$.getJSON('data.json', function (data) {
    var response = data;
    for (key in response){
        alert("key: " + key + "value :" + JSON.stringify(response[key]));
    }
});

or use response[key].ball, response[key].hat

Comments

1

The stringify function of JSON comes to rescue here. Since most internal functions take data parameters as string or sometimes buffer. Thus you can use the following code:-

    var response = {
    "Player1": {
        "ball": 1,
        "hat": 2

    },
    "Player2": {
        "ball": 1,
        "hat": 2,
    }
}

var dataString = JSON.stringify(response);

Now use dataString for sending and receiving over different calls.

Comments

0

Here's one way of printing the content of the object. Object.keys() method is used to access player's "items" and get those in array format. Updated.

var response = {
    "Player1": {
        "ball": 1,
            "hat": 2

    },
        "Player2": {
        "ball": 1,
            "hat": 2,
    }
};
for (player in response) {

    var items = Object.keys(response[player]);

    var itemText = "";
    for (i = 0; i < items.length; i++) {
        itemText += " '" + items[i] +  "'";
    }

    console.log(player +  " -" + itemText);
    
    //alternative way, suggested by NickCraver
    console.log(player +  " - "+ Object.keys(response[player]).map(function(k) { return '\''+k+'\''; }).join(' ') );
}

2 Comments

If you require quoting of each value, you should checkout .map(), like this: Object.keys(response[key]).map(function(k) { return '\''+k+'\''; }).join(' '));
@NickCraver Thanks for the tip! I'll definitely try to use .map() function in the future - looks very handy indeed :-)

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.