Getting specific field values from Json Python - python

I have a JSON file, and what I am trying to do is getting this specific field '_id'. Problem is that when I use json.load('input_file'), it says that my variable data is a list, not a dictionary, so I can't do something like:
for value in data['_id']:
print(data['_id'][i])
because I keep getting this error: TypeError: list indices must be integers or slices, not str
What I also tried to do is:
data = json.load(input_file)[0]
It kinda works. Now, my type is a dictionary, and I can access like this: data['_id']
But I only get the first '_id' from the archive...
So, what I would like to do is add all '_id' 's values into a list, to use later.
input_file = open('input_file.txt')
data = json.load(input_file)[0]
print(data['_id'])# only shows me the first '_id' value
Thanks for the help!
[{
"_id": "5436e3abbae478396759f0cf",
"name": "ISIC_0000000",
"updated": "2015-02-23T02:48:17.495000+00:00"
},
{
"_id": "5436e3acbae478396759f0d1",
"name": "ISIC_0000001",
"updated": "2015-02-23T02:48:27.455000+00:00"
},
{
"_id": "5436e3acbae478396759f0d3",
"name": "ISIC_0000002",
"updated": "2015-02-23T02:48:37.249000+00:00"
},
{
"_id": "5436e3acbae478396759f0d5",
"name": "ISIC_0000003",
"updated": "2015-02-23T02:48:46.021000+00:00"
}]

You want to print the _id of each element of your json list, so let's do it by simply iterating over the elements:
input_file = open('input_file.txt')
data = json.load(input_file) # get the data list
for element in data: # iterate on each element of the list
# element is a dict
id = element['_id'] # get the id
print(id) # print it
If you want to transform the list of elements into a list of ids for later use, you can use list comprehension:
ids = [ e['_id'] for e in data ] # get id from each element and create a list of them

As you can see the data is a list of dictionaries
for looping over data you need to use the following code
for each in data:
print each['_id']
print each['name']
print each['updated']

it says that my variable data is a list, not a dictionary, so I can't do something like:
for value in data['_id']:
print(data['_id'][i])
Yes, but you can loop over all the dictionaries in your list and get the values for their '_id' keys. This can be done in a single line using list comprehension:
data = json.load(input_file)
ids = [value['_id'] for value in data]
print(ids)
['5436e3abbae478396759f0cf', '5436e3acbae478396759f0d1', '5436e3acbae478396759f0d3', '5436e3acbae478396759f0d5']
Another way to achieve this is using the map built-in function of python:
ids = map(lambda value: value['_id'], data)
This creates a function that returns the value of the key _id from a dictionary using a lambda expression and then returns a list with the return value from this function applied on every item in data

Related

how to get access inner array of an object in dictionary

I have a json file with this structure:
[
{
"_id": "62b2ebff955fe1001d225781",
"datasetName": "comments",
"action": "dataset",
"comment": "Initial data!",
"instances": [
"62b2eb94955fe1001d22576a",
"62b2eba1955fe1001d22576e",
"62b2eba9955fe1001d225770",
"62b2ebb9955fe1001d225772",
"62b2ebcc955fe1001d225774",
"62b2ebe2955fe1001d225778"
],
"label": [
"Contemporary",
"Tango"
]
}
]
I wanted to know how can I access the values of "label" and also how can I count the length of "instances" object.
So your json is a list of dict. You can load it with the json standard library. Then you can iterate over the elements in the list. As each of these elements is a dict item, you can then get the instances list by it's key. As it is a list, you can simple call the len() function to get the length.
See the code below as an example:
import json
with open("somefile.json") as infile:
data = json.load(infile)
for element in data:
print(len(element["instances"]))
Note that if your json file contains more elements in the root list, it will of course print out the length of the instances list for each of these elements.

Return value of key from dictionary nested in a list Python

I have the following variable in my python script:
response = {"tasks": [{"attachments": [{"id": "id123xyz", "type": "ElasticNetworkInterface", "status": "PRECREATED", "details": [{"name": "john doe"}] }]}]}
I am simply trying to return the value of status. However, I'm unable to do so.
You need to index into each dictionary and list that the status is inside.
You can do the following:
# Prints "PRECREATED"
print(response["tasks"][0]["attachments"][0]["status"])
you need to access the dictionary and the list that is inside:
status = response['tasks'][0]['attachments'][0]['status']
#output
'PRECREATED'
In order to retrieve the status, you can do this
response['tasks'][0]['attachments'][0]['status']
If you would like to extract to dictionary or array of id and status then we can do:
dict([(attachment['id'], attachment['status']) for task in response['tasks'] for attachment in task['attachments']])
returns:
{'id123xyz': 'PRECREATED'}

Nested dictionary access is returning a list

The issue I'm having is that when i try to access the values within a nested dictionary, i cannot because it's returning a list instead of a dictionary.
I have a .json file with this format;
{
"users": [
{
"1": {
"1": "value",
"2": "value"
}
}
]
}
I load the .json file, and access the value i want by using this function
def load_json(fn):
with open(fn) as pf:
data = json.load(pf)
return data['users']['1']['2']
If i simply do return data it is a dictionary, but if try to access further by adding ['users'], it turns into a list and will give an index error if i try to access key #1 or #2 inside of that..
My objective is to obtain the value of the nested key #2 for example, ideally without having loop through it.
Your JSON contains an array (Python list) wrapping the inner dicts (that's what the [ and ] in your JSON indicate). All you need to do is change:
return data['users']['1']['2']
to:
return data['users'][0]['1']['2']
# ^^^ Added
to index the list to get into the inner dicts.
given your data structure, and following it down :
data is a dictionary - with one key 'users' and a value of a list
data['users'] is a list - with one entry
data['users'][0] is a dictionary - with one key '1' and a value of a dictionary
data['users'][0][1] is a dictionary - with two keys '1' and '2'
So you need to do do :
def load_json(fn):
with open(fn) as pf:
data = json.load(pf)
return data['users'][0]['1']['2']

How to access the value of 1st key of all the dictionaries saved as a list for a key in JSON using PYTHON

I have the JSON which looks like this:-
{
"name": "PT",
"batservers": [
{"name": "bat1", "qmchannel": "abcd", "mount": "efgh"},
{"name": "bat2", "qmchannel": "abcd", "mount": "efgh"},
{"name": "bat3", "qmchannel": "abcd", "mount": "efgh"},
]
}
I want to retrieve the value of "name" present in all the dictionary and save it in a list variable i.e. ["bat1","bat2","bat3"]
I tried something like this:-
batsList = env["batservers"][0:]["name"]
but it displays the below error:-
TypeError: list indices must be integers or slices, not str
I know I can do this using a loop but can someone please help me to do using in a single line code way which I am trying above?
Thanks,
SUYASH GUPTA.
You can't do it without a loop. But the loop can be a list comprehension:
batsList = [b['name'] for b in env["batservers"]
How about saving the list as:
list_of_names = [x[name] for x in env["batservers"]]
Try this:
[b['name'] for b in env['batservers']]
Or this:
map(lambda b: b['name'], env['batservers'])
[0:] doesn't do much for you: it returns the same array of dictionaries.
env["batservers"][0:] returns a list, and you can't access directly the values of the dicts in the list.
You can use a list comprehension:
names = [elem["name"] for elem in env["batservers"]]
This is a basic solution using a for loop to iterate over the sub dictionary and appending to the empty list res.
res = []
for item in env["batservers"]:
res.append(item["name"])
print (res) #=> ['bat1', 'bat2', 'bat3']

Pymongo:iterating dictionary of a list of dictionary

Let us say I have a big collection including documents such as these:
{"_id": 0,
"name":"John Doe",
"items": [
{"x":5,
"y":8,
"z":9},
{"x":4,
"y":2,
"z":1},
{"x":3,
"y":5,
"z":8}
]
}
I am fetching the collection into a variable called 'data' with pymongo and trying to iterate over the items in order to update some. I have tried to print those values with the following code:
for i in data:
for j in data[i].get("items"):
pprint(j.get("x"))
and it gave an error:
pymongo.errors.InvalidOperation: cannot set options after executing query
Even if I get the items, I do not see a way to change data. Why is this happening and how can I iterate through items and change their values?
How looks db query?
Try something like this for iteration:
cursor = db.get_collection('some_collection').find()
for doc in cursor:
for item in doc['items']:
pprint(item.get('x'))
Let's say if you want to change the values of all x.
for i in data.get('items'):
# to change the values in items
# this will replace all values of key x
i['x'] = new_value
simply assign a new value to items key.

Categories

Resources