Home > Enterprise >  Access array in json file in python
Access array in json file in python

Time:03-02

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.

CodePudding user response:

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.

CodePudding user response:

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

import json

def get_status_2_for_id(filename, id_):
    counter = 0
    with open(filename) as jdata:
        for e in json.load(jdata):
            if e.get('id', None) == id_:
                for f in e.get('evses', []):
                    if f.get('status', None) == 2:
                        counter  = 1
                break
    return counter

print(get_status_2_for_id('sample.json', 10001))

Output:

3

CodePudding user response:

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:

}]
  • Related