0

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

3
  • Welcome to SO. Just to be clear you want to find the dictionary (or Json object) with key person2? Commented Dec 25, 2020 at 17:16
  • I want to find the Object Commented Dec 25, 2020 at 17:17
  • Once you have used a JSON library to parse the data, it is the same as if you had gotten those objects in any other way. Commented Dec 25, 2020 at 17:24

4 Answers 4

2

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'}]}
Sign up to request clarification or add additional context in comments.

Comments

1

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]

Comments

0

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.

Comments

-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.

1 Comment

this is a really inefficient way of looping through if you want to use an index. enurerate() would be the way to go here

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.