0

I'm getting json data from REST API having the structure below. The data provides me a list of data having dict structure and having ids. I want to access specific data just by using the id e.g data['x1234']. But I couldn't because each time I have to iterate over the list and checking ids or ending having sth like this data["body"][0]["id"]. How can I solve the issue?

{
body:
    [{
        id: x1234,
        name: a1,
        ....
    },
    {
        id: b1234,
        name: b1,
        ....
    }]
}

I just want to extract particular object with id having a value b1234 so data["b1234"] returns the following output:

{
 id: b1234,
 name: b1,
 ....
}
Dhia
  • 10,119
  • 11
  • 58
  • 69
neonix
  • 11
  • 3
  • 1
    What have you tried, and what exactly is the problem with it? Also, *why is that the file to start with*, rather than e.g. serialising a single JSON *array*? – jonrsharpe Jan 22 '18 at 15:12
  • I'm getting json data from REST API, with above structure, I have used urllib to extract the json data, i'm unable to find syntax to extract the contents with key=value – neonix Jan 22 '18 at 15:16
  • That doesn't really answer either of my questions. Is that the result of *multiple REST calls* (in which case you need to rethink how *you're* storing them) or of a single call (in which case you need to complain to whoever maintains the service that they're returning broken JSON)? – jonrsharpe Jan 22 '18 at 15:17
  • If it is about extracting the data from JSON using Python you may want to review [this answer](https://stackoverflow.com/a/13633860/6573902). – sophros Jan 22 '18 at 15:18
  • yes, it is just extract the data with particular id, if I use ` data["body"][0]["id"] ` it is listing value of index 0, is there a way I can mention index value which contains the id value as b1234 – neonix Jan 22 '18 at 15:25
  • Possible duplicate of [Parsing values from a JSON file?](https://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file) – kenny_k Jan 22 '18 at 15:29
  • how can I condition it to extract only single object which has value of b1234 – neonix Jan 22 '18 at 15:36
  • "I have json file with multiple nested objects". This can never happen. Ask the source to fix this. –  Jan 22 '18 at 15:36
  • @LutzHorn is the incorrect json format which i'm getting from REST API ?: `{ body: [ { id:x1234 name:a1 { version:123 appname:abc } }, { id:b1234 name:b1 { appname:testapp subapp:test2 } }] } ` – neonix Jan 22 '18 at 15:41
  • This is not valid JSON because you must use double quotes `"`. But the structure looks OK. –  Jan 22 '18 at 15:50
  • It's also missing commas and keys for the nested objects, so yeah, that's not valid JSON at all. – glibdud Jan 22 '18 at 15:51

1 Answers1

0

Build new dict structure from the list using the id as dict key as follow:

items_list = json_data['body']
items_dict = {item['id']: item for item in items_list}

Then you can access specific data using the id value as follow:

print(items_dict['b1234'])
Dhia
  • 10,119
  • 11
  • 58
  • 69