Blank256
Blank256

Reputation: 3

How do I index an item in a JSON array from Python

I was thinking to myself how one would find the position of a JSON object in a JSON array through python

for example

{
    "users": [
        {
            "person1": [
                {
                    "money": 769967967456795806758905768494685798974560,
                    "name": "person1"
                }
            ]
        },
        {
            "person2": [
                {
                    "money": 696969696969969696969696969,
                    "name": "person2"
                }
            ]
        }
    ]
}

For instance let's say I wanted to get the position of person 2 from the array in python so position 2

(I looked through websites and previous questions but couldn't figure it out)

(hello people from my last post :) )

If you need any extra materials posted please request it here

Upvotes: 0

Views: 10004

Answers (4)

mrpbennett
mrpbennett

Reputation: 1956

a better way would be to use enumerate

# not a great way of doing it
>>> index = 0

>>> for value in values:
...     print(index, value)
...     index += 1
...
0 a
1 b
2 c
# a better more efficient way of doing it using enumerate()
>>> for i, value in enumerate(json["names]):
...     print(i, value)
...
0 a
1 b
2 c

Soltution:

data = {
    "users": [
        {
            "person1": [
                {
                    "money": 769967967456795806758905768494685798974560,
                    "name": "person1"
                }
            ]
        },
        {
            "person2": [
                {
                    "money": 696969696969969696969696969,
                    "name": "person2"
                }
            ]
        }
    ]
}

for i, name in enumerate(data['users']):
    print(i,name)

Output

0 {'person1': [{'money': 769967967456795806758905768494685798974560, 'name': 'person1'}]}
1 {'person2': [{'money': 696969696969969696969696969, 'name': 'person2'}]}

with enumerate, there is much less code.

Then you could do something like person2 = data['users'][1]

Upvotes: 0

ludoscred
ludoscred

Reputation: 1

i = 0
for person in json["users"]:
    if "person2" in person:
        print(i)
        break
    i += 1

You have to break the loop otherwise "i" is gonna be the value of the whole array.

Upvotes: -1

MarianD
MarianD

Reputation: 14131

Let jsn is the name of your JSON object:

jsn = {
         "users": [
             {
                 "person1": [
                     {
                         "money": 769967967456795806758905768494685798974560,
                         "name": "person1"
                     }
                 ]
             },
             {
                 "person2": [
                     {
                         "money": 696969696969969696969696969,
                         "name": "person2"
                     }
                 ]
             }
         ]
     }

It is a dictionary:

  • the key is "users", its value is a list of (nested) dictionaries,
  • the second position in this list (counting in Python from 0) is [1].

So the solution is

>>> jsn["users"][1]
{'person2': [{'money': 696969696969969696969696969, 'name': 'person2'}]}

Upvotes: 2

Sami
Sami

Reputation: 117

Lets say you have your json in a dictionary format. If not you could use some library to convert to a dict (the json library does that)

then I would iterate over the dict and check if each element has the key, in the example "person2", if yes, we found the element and can print the index.

Here is the code:

i = 0
for person in json["users"]:
    if "person2" in person:
        print(i)
    i += 1

There is probably a much cleaner solution, but that is what I can think of.

Upvotes: 0

Related Questions