Caimen
Caimen

Reputation: 2619

How to parse JSON with number as a key

I have the following json, I don't have any control over this output unfortunately.

{
"questions": {
    "9733": {
        "text": "Star Trek or Star Wars?",
        "answers": {
            "41003": "Star Trek",
            "41004": "Star Wars",
            "41005": "Neither is superior in my opinion; both great in their own ways",
            "41006": "Not a fan",
            "41007": "I don't have an opinion on this"
        }
    },
    "25272": {
        "text": "Which of these summer movies are you looking forward to the most?",
        "answers": {
            "99545": "World War Z",
            "99546": "Monsters University ",
            "99547": "White House Down",
            "99548": "Man of Steel",
            "99549": "Lone Ranger",
            "99550": "The Wolverine"
        }
    },
    "27547": {
        "text": "Should the U.S. attack Syria?",
        "answers": {
            "107453": "Yes",
            "107454": "No"
        }
    }
}
}

I am using json.parse to parse this. To get the text of the first question I would normally do something like this.

var jsonData = JSON.parse(data);//Where data = the json above
console.log(jsonData.questions.9733.text);//Obviously this fails

However javascript doesn't like that number in there obviously. How would you recommend accessing the text of the first question? I would prefer the json to be setup better with in an array of questions instead. Unfortunately I don't have any control over the output of this JSON.

I'm also not going to be aware of the what the keys are as they come across, but thats a whole other issue. I'm willing entertain any suggestions on how to parse this thing as I've never had to parse such a strange JSON output.

Upvotes: 15

Views: 27917

Answers (5)

parthipang
parthipang

Reputation: 1

Try using Ason, If you are using Java 8. Gradle dependency compile 'org.arivu:ason:1.0.3'.

Java code as follows

Ason ason = new Ason();
Object json = ason.fromJson("<<JSON String!>>");
System.out.println(Ason.getJson(json, "questions.9733.text", null)):

Upvotes: -1

Tohveli
Tohveli

Reputation: 485

I believe you can access the data via same syntax as in an array:

console.log(jsonData.questions[9733].text);

Upvotes: 2

VictorR
VictorR

Reputation: 31

Why don't you try?

jsonData["questions"]["9733"]

How to access a numeric property?

Upvotes: 2

p.s.w.g
p.s.w.g

Reputation: 149020

You need to use bracket notation:

console.log(jsonData.questions["9733"].text);

But because the value inside the brackets will be automatically converted a string, this would also work:

console.log(jsonData.questions[9733].text);

Note however, that using non-strings is as property names generally bad form and could lead to some subtle problems, e.g. if the property name was "001", then [001] would not work.

Upvotes: 15

Kyle Paulsen
Kyle Paulsen

Reputation: 1006

If you have to use numbers as keys... you can access them like this:

var text = jsonData.questions["9733"].text;

Edit: You can also access it with the number 9733. It doesn't have to be a string. Only needs to be a string if the key is non-numeric.

Upvotes: 1

Related Questions