for an input json
[{
"Name": "John",
"Age": "23",
"Des": "SE"
},
{
"Name": "Rai",
"Age": "33",
"Des": "SSE"
},
{
"Name": "James",
"Age": "42",
"Des": "SE"
}
]
I want to filter out the json data where only "Des":"SE" is true
required output
[{
"Name": "John",
"Age": "23"
},
{
"Name": "James",
"Age": "42"
}
]
A list comprehension should do it:
out = [{'Name':d['Name'], 'Age':d['Age']} for d in lst if d['Des']=='SE']
Another way:
out = [d for d in lst if d.pop('Des')=='SE']
Output:
[{'Name': 'John', 'Age': '23'}, {'Name': 'James', 'Age': '42'}]
To make it more dynamic if each json has more elements:
import json
input_str = '[{"Name": "John", "Age": "23", "Des": "SE"}, {"Name": "Rai", "Age": "33", "Des": "SSE"}, {"Name": "James", "Age": "42", "Des": "SE"}]'
input_list = json.loads(input_str)
# If you already converted to a list of dicts, then you don't need the above
# Using pop here removes the key you are using to filter
output = [each for each in input_list if each.pop("Des") == "SE"]
using the json module, you can load a file using loads or a string using load. From there, it acts as a normal python list of dictionaries which you can iterate over and check the keys of. From there, you simply create a new list of dictionaries that match your desired pattern and remove the key you are no longer using. Example:
import json
jsonString = """[{
"Name": "John",
"Age": "23",
"Des": "SE"
},
{
"Name": "Rai",
"Age": "33",
"Des": "SSE"
},
{
"Name": "James",
"Age": "42",
"Des": "SE"
}
]"""
jsonList = json.loads(jsonString)
filteredList = []
def CheckDes(dataDict: dict):
if dataDict['Des'] == 'SE':
dataDict.pop('Des')
filteredList.append(dataDict)
print(jsonList)
"""
[
{
'Name': 'John',
'Age': '23',
'Des': 'SE'
},
{
'Name': 'Rai',
'Age': '33',
'Des': 'SSE'
},
{
'Name': 'James',
'Age': '42',
'Des': 'SE'
}
]"""
[CheckDes(dataDict) for dataDict in jsonList]
print(filteredList)
"""[
{
'Name': 'John',
'Age': '23'
},
{
'Name': 'James',
'Age': '42'
}
]
"""
Related
dict_example = {
"abc": [
{
"name": "bcd",
"gender": "male",
"options": {
"emp_id": "a10734",
"address": "cfg",
"dept": "IT",
},
}
]
}
I have above dictionary and I need to add below values to options programmatically.
"desgn":"Engineer",
"project" : "xyz",
I need output in the below formart
dict_example = {
"abc": [
{
"name": "bcd",
"gender": "male",
"options": {
"emp_id": "a10734",
"address": "cfg",
"dept": "IT",
"desgn":"Engineer",
"project" : "xyz",
},
}
]
}
Can anyone help me with the above problem it will be great!
You can simply use the keys and indices to reach the item and add or change it:
dict_example['abc'][0]['options']['design'] = 'Engineer'
dict_example['abc'][0]['options']['project'] = 'xyz'
print(dict_example)
The output will be:
{'abc': [{'name': 'bcd', 'gender': 'male', 'options': {'emp_id': 'a10734', 'address': 'cfg', 'dept': 'IT', 'design': 'Engineer', 'project': 'xyz'}}]}
I've been trying to get this to work.
There are two different JSON lists that contain different keys.
list1
[
{
"name":"John",
"measurement": "5.11"
},
{
"name":"Kate",
"measurement": "5.6"
}
]
list2
[
{
"name":"John",
"characteristics": {
"height": [
"6.0"
]
}
},
{
"name":"Mike",
"characteristics": {
"height": [
"5.10"
]
}
}
]
code
for k in list2:
if v['name'] in [key['name'] for key in list1:
list1.append(k['measurement'])
The output I get is,
[{'name': 'John', 'characteristics': ['height': '6.0', 'age': 30}, '5.11']
Expected output
[{'name': 'John', 'characteristics': ['height': '5.11', 'age': 30}]
The loops go over the keys and if the key['name'] are equals in both lists,
then it proceeds into happening that specific values from the given key.
The only problem is that it is not working correctly for me. I just want to replace that value from height in characteristics with the one in measurement.
[EDIT]:
I made changes to the json. It should be correct now. Basically, height is an array.
JSON you posted is invalid so I fixed it little bit. Following code should work:
list_1 = [
{
"name": "John",
"measurement": "5.11"
},
{
"name": "Kate",
"measurement": "5.6"
}
]
list_2 = [
{
"name": "John",
"characteristics": {
"height": "6.0",
"age": 30
}
},
{
"name": "Mike",
"characteristics": {
"height": "5.10",
"age": 25
}
}
]
result = []
for list_1_item in list_1:
single_obj = {}
for list_2_item in list_2:
if list_2_item['name'] == list_1_item['name']:
single_obj['name'] = list_1_item['name']
single_obj['characteristics'] = list_2_item['characteristics']
result.append(single_obj)
print(result)
This gives us following result:
[{'name': 'John', 'characteristics': {'height': '6.0', 'age': 30}}]
There is a nested dictionary with multiple level of keys. The requirements are:
Create nested keys by using keypath if it doesn't exist
Update the value by using the keypath if it exists
For example, this is the dictionary:
{
"animal": {
"dog": {
"type": "beagle"
}
},
"man": {
"name": "john",
"age": 36
},
"plant": {
"fruit": {
"apple": {
"type": "gala"
}
}
}
}
Here are the functions to update the value or append a new nested keypath:
appendDict(["man", "name"], "daniel", json_dict)
appendDict(["computer", "laptop", "maker"], "hp", json_dict)
Here is the expected result:
{
"animal": {
"dog": {
"type": "beagle"
}
},
"man": {
"name": "daniel",
"age": 36
},
"plant": {
"fruit": {
"apple": {
"type": "gala"
}
}
},
"computer": {
"laptop": {
"maker": "hp"
}
}
}
My question is how to implement the appendDict() function in order to support the requirements?
Here is my code so far which doesn't work yet:
json_dict = {
"animal": {"dog": {"type": "beagle"}},
"man": {"name": "john", "age": 36},
"plant": {"fruit": {"apple": {"type": "gala"}}}
}
def appendDict(keys, value, json_dict):
for index, key in enumerate(keys):
if key not in json_dict:
if index == len(keys) - 1:
some_data = {}
some_data[key] = value
json_dict[key] = some_data
else:
some_data = {}
json_dict[key] = some_data
else:
json_dict[key] = value
appendDict(["man", "name"], "daniel", json_dict)
appendDict(["computer", "laptop", "maker"], "hp", json_dict)
You can use recursion by slicing keys at every call:
def appendDict(keys, value, json_dict):
if len(keys) == 1:
json_dict[keys[0]] = value
else:
if keys[0] not in json_dict:
json_dict[keys[0]] = {}
appendDict(keys[1:], value, json_dict[keys[0]])
json_dict = {'animal': {'dog': {'type': 'beagle'}}, 'man': {'name': 'john', 'age': 36}, 'plant': {'fruit': {'apple': {'type': 'gala'}}}}
appendDict(["man", "name"], "daniel", json_dict)
appendDict(["computer", "laptop", "maker"], "hp", json_dict)
import json
print(json.dumps(json_dict, indent=4))
Output:
{
"animal": {
"dog": {
"type": "beagle"
}
},
"man": {
"name": "daniel",
"age": 36
},
"plant": {
"fruit": {
"apple": {
"type": "gala"
}
}
},
"computer": {
"laptop": {
"maker": "hp"
}
}
}
How can I flatten a List of dictionary with nested dictonaries, say I have the following dict:
data = [
{ 'Name':'xyx',
'Age':22,
'EmpDetails':{'Salary':100,'Job':'Intern','Location':'TER'}
},
{ 'Name':'abc',
'Age':23,
'EmpDetails':{'JoinDate':'20110912','Salary':200,'Job':'Intern2','Location':'TER2'}
},
{'Name':'efg',
'Age':24,
'EmpDetails':{'JoinDate':'20110912','enddate':'20120912','Salary':300,'Job':'Intern3','Location':'TER3'}
}
]
i would need the EmpDetails Node removed and move its values one level up, like below
data = [
{ 'Name':'xyx','Age':22,'Salary':100,'Job':'Intern','Location':'TER'},
{ 'Name':'abc','Age':23,'JoinDate':'20110912','Salary':200,'Job':'Intern2','Location':'TER2'},
{'Name':'efg','Age':24,'JoinDate':'20110912','enddate':'20120912','Salary':300,'Job':'Intern3','Location':'TER3'}
]
i am this now using below, is there any faster way of doing this?
newlist = []
for d in data:
empdict ={}
for key, val in d.items():
if(key!='EmpDetails'):
empdict[key] = val
if(key=='EmpDetails'):
for key2, val2 in val.items():
empdict[key2] = val2
newlist.append(empdict)
This is one approach using dict.update and .pop
Ex:
data = [
{ 'Name':'xyx',
'Age':22,
'EmpDetails':{'Salary':100,'Job':'Intern','Location':'TER'}
},
{ 'Name':'abc',
'Age':23,
'EmpDetails':{'JoinDate':'20110912','Salary':200,'Job':'Intern2','Location':'TER2'}
},
{'Name':'efg',
'Age':24,
'EmpDetails':{'JoinDate':'20110912','enddate':'20120912','Salary':300,'Job':'Intern3','Location':'TER3'}
}
]
for i in data:
i.update(i.pop("EmpDetails"))
print(data)
Output:
[{'Age': 22, 'Job': 'Intern', 'Location': 'TER', 'Name': 'xyx', 'Salary': 100},
{'Age': 23,
'Job': 'Intern2',
'JoinDate': '20110912',
'Location': 'TER2',
'Name': 'abc',
'Salary': 200},
{'Age': 24,
'Job': 'Intern3',
'JoinDate': '20110912',
'Location': 'TER3',
'Name': 'efg',
'Salary': 300,
'enddate': '20120912'}]
One line method, maybe a little tricky.
data = [
{
"Name": "xyx",
"Age": 22,
"EmpDetails": {"Salary": 100, "Job": "Intern", "Location": "TER"},
},
{
"Name": "abc",
"Age": 23,
"EmpDetails": {
"JoinDate": "20110912",
"Salary": 200,
"Job": "Intern2",
"Location": "TER2",
},
},
{
"Name": "efg",
"Age": 24,
"EmpDetails": {
"JoinDate": "20110912",
"enddate": "20120912",
"Salary": 300,
"Job": "Intern3",
"Location": "TER3",
},
},
]
# only python3.5+
res = [{**item.pop("EmpDetails", {}), **item} for item in data]
I'd prefer using json_normalize() method from pandas library since it would be an elegant solution and had no effect on readability of your code.
Examples can bee seen here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.io.json.json_normalize.html
I am trying to read a JSON file and iterate through it, for instance I am trying to print the children, or the firstname, or the hobbies etc...
The JSON file looks like this:
{
"firstName": "Jane",
"lastName": "Doe",
"hobbies": ["running", "sky diving", "singing"],
"age": 35,
"children": [
{
"firstName": "Alice",
"age": 6
},
{
"firstName": "Bob",
"age": 8
}
]
},
{
"firstName": "Mike",
"lastName": "Smith",
"hobbies": ["bowling", "photography", "biking"],
"age":40,
"children": [
{
"firstName": "Steve",
"age": 10
},
{
"firstName": "Sara",
"age": 18
}
]
}
I'm loading the json file using the following code:
import json
with open('test.json') as f:
data = json.load(f)
and I can print parts of the first record fine like this:
print(data['children'])
print(data['hobbies'])
[{'firstName': 'Alice', 'age': 6}, {'firstName': 'Bob', 'age': 8}]
['running', 'sky diving', 'singing']
I'd like to iterate through the records though so I can print pieces of the 2nd entry as well (or 3rd, or 20th if applicable)
When I try this however:
for key, value in data.items():
print(key, value)
It still just returns the first entry:
firstName Jane
lastName Doe
hobbies ['running', 'sky diving', 'singing']
age 35
children [{'firstName': 'Alice', 'age': 6}, {'firstName': 'Bob', 'age': 8}]
Any ideas?
The Problem you are facing is you are having the data as a single json.
You need to make it as an array. Something like this.
[{ // notice additional [
"firstName": "Jane",
"lastName": "Doe",
"hobbies": ["running", "sky diving", "singing"],
"age": 35,
"children": [
{
"firstName": "Alice",
"age": 6
},
{
"firstName": "Bob",
"age": 8
}
]
},
{
"firstName": "Mike",
"lastName": "Smith",
"hobbies": ["bowling", "photography", "biking"],
"age":40,
"children": [
{
"firstName": "Steve",
"age": 10
},
{
"firstName": "Sara",
"age": 18
}
]
}] // notice additional ]
Then you need to loop it over the list and then as per what you have written. Something like this
import json
with open('abc.json') as f:
data = json.load(f)
for element in data:
for key, value in element.items():
print(key, value)
To covert your file be more std JSON, and open it add [ and ], to make it as json list. and whole code paste below:
import json
f = open("test_json.txt", "r")
contents = f.readlines()
f.close()
contents.insert(0, "[")
f = open("test_json.txt", "w")
contents = "".join(contents)
f.write(contents)
f.write("]")
f.close()
with open("test_json.txt", "r") as fd:
d = json.load(fd)
for i in d:
print(i)