0

Here is a question from an absolute beginner python developer. Here is the challenge I have :)

not being able to access the "status" in this json file:

[{
        "id": 0,
        "sellerId": "HHH",
        "lat": 90.293846,
        "lon": 15.837098,
        "evses": [{
                "id": 0,
                "status": 1,
                "connectors": [{
                        "type": "Hyyyyp",
                        "maxKw": 22
                    }
                ]
            }, {
                "id": 2001,
                "status": 2,
                "connectors": [{
                        "type": "Hyyyyp",
                        "maxKw": 22
                    }
                ]
            }, {
                "id": 2002,
                "status": 1,
                "connectors": [{
                        "type": "Hyyyyp",
                        "maxKw": 22
                    }
                ]
            }, {
                "id": 2003,
                "status": 1,
                "connectors": [{
                        "type": "Hyyyp",
                        "maxKw": 22
                    }
                ]
            }
        ] 

       }, {
            "id": 10001,
            "sellerId": 7705,
            "lat": 12.59962,
            "lon": 40.8767,
            "evses": [{
                    "id": 10001,
                    "status": 1,
                    "connectors": [{
                            "type": "Tyyyyp",
                            "maxKw": 22
                        }
                    ]
                }, {
                    "id": 10002,
                    "status": 2,
                    "connectors": [{
                            "type": "Tyyyyp",
                            "maxKw": 22
                        }
                    ]
                }, {
                    "id": 10003,
                    "status": 2,
                    "connectors": [{
                            "type": "Tyyyyp",
                            "maxKw": 22
                        }
                    ]
                }, {
                    "id": 10004,
                    "status": 2,
                    "connectors": [{
                            "type": "Tyyyyp",
                            "maxKw": 22
                        }
                    ]
                }
            ]
        }, {

for the "id:10001" there are 3 cases which "status: 2". So.. how do I print 3 for id:10001?

I guess I need to have an array for storying the ids itself and another array for storying the number of "status:2" for each id.

Here is my code: firs I do print id:

with open('sample.json') as f:
    data = json.load(f)
    print(id['id'])

Then I think I need to access array evses: So here is what I do:

print(data['evses'][0]['id']['status'])

But I get error on this line.

8
  • 2
    data['evses'][0] is a dictionary, data['evses'][0]['id'] is 10001, it has no 'status' key Commented Mar 2, 2022 at 10:03
  • 1
    Your example won't work because status is not a key of id. dataID = data['evses'][0]['id'] dataStatus = data['evses'][0]['status'] Commented Mar 2, 2022 at 10:04
  • so how do I access the status? is not it through evses array? Commented Mar 2, 2022 at 10:09
  • 1
    Please show more of the data so we can show you how to get at those values Commented Mar 2, 2022 at 10:14
  • 1
    id 10001 has a status of 1. ids 10002, 10003 and 10004 have statuses of 2. So why would you want 3 for id 10001? Maybe you just want to know how many ids have a particular status value? Commented Mar 2, 2022 at 10:27

3 Answers 3

1

Following clarification from OP, this would be my proposed solution:

import json
from collections import Counter

def get_status_2_for_id(filename):
    count = Counter()
    with open(filename) as jdata:
        for e in json.load(jdata):
            if (id_ := e.get('id')) is not None:
                for f in e.get('evses', []):
                    if f.get('status') == 2:
                        count[id_] += 1
    return count.items()

for id_, count in get_status_2_for_id('sample.json'):
    print(f'id={id_} count={count}')

Output:

id=0 count=1
id=10001 count=3
Sign up to request clarification or add additional context in comments.

4 Comments

It does not print out the output for me!
@user2758510 I created sample.json from the data you've shown and it works perfectly
are you sure that you got "output" using this code? It does not work for me!
If you're not getting any output then that's because your data doesn't match that shown in your question
1

Let's say you take a single JSON record of your data which is below

record = {
    "id": 10001,
    "sellerId": 7705,
    "lat": 12.59962,
    "lon": 40.8767,
    "evses": [{
        "id": 10001,
        "status": 1,
        "connectors": [{
            "type": "Tyyyyp",
            "maxKw": 22
        }
        ]
    }, {
        "id": 10002,
        "status": 2,
        "connectors": [{
            "type": "Tyyyyp",
            "maxKw": 22
        }
        ]
    }, {
        "id": 10003,
        "status": 2,
        "connectors": [{
            "type": "Tyyyyp",
            "maxKw": 22
        }
        ]
    }, {
        "id": 10004,
        "status": 2,
        "connectors": [{
            "type": "Tyyyyp",
            "maxKw": 22
        }
        ]
    }
    ]
}

From this data record above, if you want to count the number of occurences of a particular status you can do something like below

status_2_count = [stp["status"] for stp in record["evses"]].count(2)

We just generate a list of all statuses in the record["evses"] and count the occurence of a particualr status.

You can make this a function, and repeat it for other records in the file.

1 Comment

Tried it but did not work. I got this error: status_2_count = [stp["status"] for stp in id["evses"]].count(2) TypeError: 'builtin_function_or_method' object is not subscriptable
1

You can try this if its stored as a variable:

for status in json_data["evses"]:
    print('status = ', status['status'])

And this if it's stored in a file:

import json

status_pts = 1

with open('file.json') as json_file:
    data = json.loads(json_file.read())
    ls = data[0]['evses']
    for s in ls:
        if s['status'] == status_pts:
            print('id:', s['id'], "number of status =", status_pts)

Also, your json data wasn't closed off, the very last line has:

}, {

It needed:

}]

7 Comments

I get "TypeError" error in the for loop line
Is the data assigned to a variable? I put your json data into a variable called json_data.
The json file starts exactly with what I have in my question which is [{ is there anything missing you think?
Then how do I iterate through the ids and print out the number of status that are equal to 2 for each id? for example I would like to print out something like this: id = 0 , 1(number of status = 2) and id:10001 , 3(number of status = 2)
I'm not real sure what you're after there. I have tried understanding your new question and have edited my original. If my answer helped your original question, please mark it as the answer.
|

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.