0

All the doc I find online relies on a for loop to navigate in a json in Python. Is there no other faster choice to perform the following action? Something like myjson["values"][:]["id"]? Thanks.

MWE:

import json
myjson = json.loads(str('{"total" : 3, "values" : [{"id": 10100,"name": "extra name"},{"id": 10101,"name": "awesome name"},{"id": 10102,"name": "strange name"}]}'))

# I want to print the json's id's, only method?
for i in myjson["values"]:
    print(i["id"])
Odyseus_v4
  • 195
  • 1
  • 11
  • What exactly is wrong with `[x['id'] for x in myjson['values']]`? What actual evidence do you have that something "faster" is needed? I suppose you could do something like `list(map(lambda x: x['id'], myjson['values']))` or `from operator import itemgetter; list(map(itemgetter('id'), myjson['values']))`. But really, why bother? A list-comp or for-loop is much more readable. – ekhumoro Oct 03 '22 at 12:58
  • I am mostly wondering if there is something similar to the jq syntax, basically being able to navigate seamlessly in a json tree. And extract all keys of an array in a single line. `[x['id'] for x in myjson['values']]` do serves that purpose somehow. As for it being slower, infact for my curent purpose I do not need something faster. But I have read that for loops are generally things to be avoided if another solution exists, since they are iterrating through all members of the list. I wanted to know if that something exists. – Odyseus_v4 Oct 05 '22 at 13:22
  • Does this answer your question? [Are list-comprehensions and functional functions faster than "for loops"?](https://stackoverflow.com/questions/22108488/are-list-comprehensions-and-functional-functions-faster-than-for-loops) – ekhumoro Oct 05 '22 at 13:47
  • There's nothing special about json trees. They're composed of normal python dicts, lists, etc. So what you're asking is whether for-loops are the most efficient way to extract elements from dicts, lists, etc. There's no general answer to that: it depends on the context. See the linked question above for more details. There's no json-specific solution that will be any faster, since it must use the same underlying functionality. – ekhumoro Oct 05 '22 at 13:48

1 Answers1

0

You almost made it. This is how you do it. You need to assign the return of json.loads to your myjson variable. You cannot use linebreaks between json and loads, it must be json.loads because loads is a function in the json module.

import json

my_json = '{"total" : 3, "values" : [{"id": 10100,"name": "extra name"},{"id": 10101,"name": "awesome name"},{"id": 10102,"name": "strange name"}]}'
data = json.loads(my_json)

for i in data["values"]:
    print(i["id"])
Fred
  • 12,086
  • 7
  • 60
  • 83
  • Hi, yeah sorry I copy pasted wrongly. But sorry, my question is not "how to make the for loop" work but "is the for loop the only way of navigating through a json?". Thanks for your reply still – Odyseus_v4 Oct 03 '22 at 10:28
  • 1
    Well it depends on the JSON object, the `values` key in your JSON is an list/array, so you probably want to iterate over it with a for loop. Perhaps you could also use the `map` function for a more functional programming approach. – Fred Oct 03 '22 at 12:04