I have the following json file, Need to loop over to fetch the company and its file.
data.json
[
{
"count": 0,
"company": "abc",
"state": {
"city": {}
},
"name": "Techno",
"age": 17,
"file": "cubix.so"
},
{
"count": 12,
"company": "def",
"state": {
"city": {}
},
"name": "Uni",
"age": 17,
"file": "cyp.so"
},
{
"count": 22,
"company": "ghi",
"state": {
"city": {}
},
"name": "unicorn",
"age": 17,
"file": "omg.so"
}
]
Need to loops through json file and print the file with its company.
import json
import sys
f=open('data.json')
print(json.dumps(output))
Expected Output:
file with respect to company
Use json.loads() to load from JSON content and context managers while working with files:
import json
with open('data.json') as file:
data = json.loads(file.read())
for obj in data:
print(obj['company'], obj['file'])
Output:
abc cubix.so
def cyp.so
ghi omg.so
After reading the file, you could use json.load() to load the object, which is a list. Then you can iterate over the list and print the respective keys 'company' and 'file'
import json
f = open('data.json')
for e in json.load(f):
print(e['company'], e['file'])
f.close()
output
abc cubix.so
def cyp.so
ghi omg.so
Related
I have two JSON files named new and old that files have some data. here I want to compare new.json with the old.json file while comparing if I have the same data in those two JSON files I don't want to create any new JSON file
If I have different data like below in new.json and old.json
new.json:
[
{
"name": "Mohan raj",
"age": 23,
"country": "INDIA"
},
{
"name": "Kiruthika",
"age": 18,
"country": "INDIA"
},
{
"name": "Munusamy",
"age": 45,
"country": "INDIA"
},
{
"name": "John Wood",
"age": 35,
"country": "USA"
},
{
"name": "Mark Smith",
"age": 25,
"country": "USA"
}
]
old.json:
[
{
"name": "John Wood",
"age": 35,
"country": "USA"
},
{
"name": "Mark Smith",
"age": 30,
"country": "USA"
},
{
"name": "Oscar Bernard",
"age": 25,
"country": "Australia"
}
]
If the new.json file has any of the same data of old.json having we have to skip that data and the new.json file have any of the updated data of old.json having and the new data's in new.json we have to create a new JSON file named updated.json with the data of the above scenarios.
The resulted JSON file needs to look like this:
updated.json:
[
{
"name": "Mohan raj",
"age": 23,
"country": "INDIA"
},
{
"name": "Kiruthika",
"age": 18,
"country": "INDIA"
},
{
"name": "Munusamy",
"age": 45,
"country": "INDIA"
},
{
"name": "Mark Smith",
"age": 25,
"country": "USA"
}
]
Took me a while to get, thanks for answering my questions, and it seems like "updated" might simply be expressed as "new not in old"?
I think so, because the following seems to do the job.
The key is to make comparisons of the objects themselves, and not wanting to get into object comparison (deep-equal), just hashing each object back to JSON gives us string representations we can compare:
import json
old_hashes = []
old_objs = json.load(open('old.json'))
for old_obj in old_objs:
old_hash = json.dumps(old_obj)
old_hashes.append(old_hash)
# "Updated" means "new not in old"
updated_objs = []
new_objs = json.load(open('new.json'))
for new_obj in new_objs:
new_hash = json.dumps(new_obj)
if new_hash not in old_hashes:
updated_objs.append(new_obj)
print(json.dumps(updated_objs, indent=2))
When I run that against your old.json and new.json, I get:
[
{
"name": "Mohan raj",
"age": 23,
"country": "INDIA"
},
{
"name": "Kiruthika",
"age": 18,
"country": "INDIA"
},
{
"name": "Munusamy",
"age": 45,
"country": "INDIA"
},
{
"name": "Mark Smith",
"age": 25,
"country": "USA"
}
]
You can achieve it below. keep track of data from old json.
import json
# read json file
with open('new.json') as f:
new_data = json.load(f)
with open('old.json') as f:
old_data = json.load(f)
old_json_list = [
{elem["name"], elem["age"], elem["country"]} for elem in old_data]
updated_list = []
for elem in new_data:
elm = {elem["name"], elem["age"], elem["country"]}
if elm not in old_json_list:
updated_list.append(elem)
with open('updated.json', 'w') as f:
json.dump(updated_list, f)
Im getting this error while executing my code TypeError: string indices must be integers. Before that im doing the same thing but its working.
for con in bin3 ['content']['annotations']:
print(con) #This loop is working fine.
This is my python code
import pandas as pd
import json
filename = 'new4.json'
for line in open(filename, 'r'):
print(line)
for item in filename['content']['transform']: #this loop is not working
print(item)
This is my json file
{
"content": {
"transform": [
{
"ID": 5,
"class": 6,
"createTime": "",
"name": "Source",
"advancedProperties": [
{
"ID": 82,
"class": 12,
"name": "Tracing",
"value": "Normal"
}]
}]
}
}
Do this:
Correct way to use a JSON file in Python is to convert it into a Dictionary first - json.load() does it for you
import json
filename = 'new4.json'
with open(filename) as json_file:
data = json.load(json_file)
print(data['content']['transform'])
NOTE: Your Json file is not in Right Format - Closing Braces are missing
Correct JSON Format:
{
"content": {
"transform": [
{
"ID": 5,
"class": 6,
"createTime": "",
"name": "Source",
"advancedProperties": [
{
"ID": 82,
"class": 12,
"name": "Tracing",
"value": "Normal"
}
]
}
]
}
}
I'm exporting a dataframe to a JSON file, through these lines of code:
with open('example.json', 'w') as f:
for row in df3.iterrows():
row[1].to_json(f, orient=None, lines=False)
f.write("\n")
And it returns a file like this:
{"age":20,"city":"Burdinne","email":"enim#risus.org","name":"Zorita","phone":4565434645.0,"postal_code":42680.0,"regDate":"2015-06-14T12:12:00-07:00"}
{"age":22,"city":"Bharatpur","email":"purus.mauris.a#odiosagittis.ca","name":"Mariam","phone":null,"postal_code":null,"regDate":"2016-10-14T18:52:48-07:00"}
{"age":28,"city":"Neerheylissem","email":"Nam#enimEtiam.org","name":"Malik","phone":null,"postal_code":null,"regDate":"2016-09-20T18:06:55-07:00"}
{"age":24,"city":"San Fratello","email":"sapien#Nullamlobortis.ca","name":"Claire","phone":null,"postal_code":null,"regDate":"2016-12-29T09:49:13-08:00"}
{"age":30,"city":"La Cruz","email":"tempor#purusmaurisa.edu","name":"Hilel","phone":null,"postal_code":null,"regDate":"2016-07-09T12:03:31-07:00"}
However, I would like that JSON file to be tabulated like this:
[
{
"name": "Zorita",
"email": "enim#risus.org",
"regDate": "2015-06-14T12:12:00-07:00",
"city": "Burdinne",
"age": 20,
"postal_code":42680,
"phone": 4565434645
},
{
"name": "Mariam",
"email": "purus.mauris.a#odiosagittis.ca",
"regDate": "2016-10-14T18:52:48-07:00",
"city": "Bharatpur",
"age": 22
},
{
"name": "Malik",
"email": "Nam#enimEtiam.org",
"regDate": "2016-09-20T18:06:55-07:00",
"city": "Neerheylissem",
"age": 28
},
{
"name": "Claire",
"email": "sapien#Nullamlobortis.ca",
"regDate": "2016-12-29T09:49:13-08:00",
"city": "San Fratello",
"age": 24
},
{
"name": "Hilel",
"email": "tempor#purusmaurisa.edu",
"regDate": "2016-07-09T12:03:31-07:00",
"city": "La Cruz",
"age": 30
}
]
How could I do this?
In my code I'm trying to put the line break with "\ n" but apparently I'm not doing it correctly
Try below code:
final_list = list()
for row in df3.iterrows():
final_list.append(row[1].to_dict(orient=None))
with open('example.json', 'w') as f:
f.write(json.dumps(final_list, indent=4))
You can convert column to list and write to file by json.dump with parameter indent and if necessary sort_keys=True for pretty json:
import json
with open("example.json", "w") as f:
json.dump(df[1].tolist(), f, indent=4, sort_keys=True)
Sample:
d = [
{
"name": "Zorita",
"email": "enim#risus.org",
"regDate": "2015-06-14T12:12:00-07:00",
"city": "Burdinne",
"age": 20,
"postal_code":42680,
"phone": 4565434645
},
{
"name": "Mariam",
"email": "purus.mauris.a#odiosagittis.ca",
"regDate": "2016-10-14T18:52:48-07:00",
"city": "Bharatpur",
"age": 22
}
]
df = pd.DataFrame({1: d})
#print (df)
import json
with open("example.json", "w") as f:
json.dump(df[1].tolist(), f, indent=4, sort_keys=True)
[
{
"age": 20,
"city": "Burdinne",
"email": "enim#risus.org",
"name": "Zorita",
"phone": 4565434645,
"postal_code": 42680,
"regDate": "2015-06-14T12:12:00-07:00"
},
{
"age": 22,
"city": "Bharatpur",
"email": "purus.mauris.a#odiosagittis.ca",
"name": "Mariam",
"regDate": "2016-10-14T18:52:48-07:00"
}
]
Although this has been answered by #skaul05, using iterrows can be inefficient.
This might be better
with open('file.json', 'w') as f:
f.write(json.dumps(json.loads(df.to_json()), indent=4))
I am trying to convert my json data into a dictionary with key the id of the json data. for example lets say i have the following json:
{
"id": "1",
"name": "John",
"surname": "Smith"
},
{
"id": "2",
"name": "Steve",
"surname": "Ger"
}
And i want to construct a new dictionary which includes the id as a key and save it into a file so i wrote the following code:
json_dict = []
request = requests.get('http://example.com/...')
with open("data.json", "w") as out:
loaded_data = json.loads(request.text)
for list_item in loaded_data:
json_dict.append({"id": list_item["id"], "data": list_item })
out.write(json.dumps(json_dict))
In the file i get the following output:
[{"data": {"id":"1",
"name":"John",
"Surname":"Smith"
}
},
{"data": {"id":"2",
"name":"Steve",
"Surname":"Ger"
}
},
]
Why the id is not included in my dict before data ?
I'm pretty sure you're looking at a ghost here. You probably tested wrong. It will go away when you try to create a minimal, complete, and verifiable example for us (i. e. with a fixed string as input instead of a request call, with a print instead of an out.write, etc).
This is my test in which I could not reproduce the problem:
entries = [
{
"id": "1",
"name": "John",
"surname": "Smith"
},
{
"id": "2",
"name": "Steve",
"surname": "Ger"
}
]
json_dict = []
for i in entries:
json_dict.append({"id":i["id"], "data": i})
json.dumps(json_dict, indent=2)
This prints as expected:
[
{
"id": "1",
"data": {
"id": "1",
"surname": "Smith",
"name": "John"
}
},
{
"id": "2",
"data": {
"id": "2",
"surname": "Ger",
"name": "Steve"
}
}
]
You could try this instead:
json_dict = []
with open('data.json', 'w) as f:
loaded_data = json.loads(request.text)
for list_item in loaded_data:
json_dict.append({list_item['id'] : list_item})
I have a sample JSON in this format:
JSON FILE:
{
"Name": "ABC",
"Phone":"123",
"Address":[{"City":"City-1"},{"Country":"Country-1"}]
}
{
"Name": "ABC-1",
"Phone":"123-1",
"Address":[{"City":"City-2"},{"Country":"Country-2"}]
}
Is there any approach to parse the JSON and loop through the file and print each key-value pair.
The approach I used was through using
json_open = open(json_file)
json_data = json.load(json_open)
print(json_data[Name]) ##should give ABC
print(json_data[Name]) ##should give ABC-1 - unsure about the syntax and format
But I'm currently able to print only the first object values - i.e. name=ABC and not name=ABC-1
There is error in your json file. I modified your json and written code for traverse each element in it.
Error:
Error: Parse error on line 9:
... "Country-1" }]}{ "Name": "ABC-1",
-------------------^
Expecting 'EOF', '}', ',', ']', got '{'
sample.json
{
"data": [
{
"Name": "ABC",
"Phone": "123",
"Address": [
{
"City": "City-1"
},
{
"Country": "Country-1"
}
]
},
{
"Name": "ABC-1",
"Phone": "123-1",
"Address": [
{
"City": "City-2"
},
{
"Country": "Country-2"
}
]
}
]
}
sample.py
import json
json_file='sample.json'
with open(json_file, 'r') as json_data:
data = json.load(json_data)
jin=data['data']
for emp in jin:
print ("Name :"+emp["Name"])
print ("Phone :"+emp["Phone"])
print ("City :"+emp["Address"][0]["City"])
print ("Country :"+emp["Address"][1]["Country"])
Each record on json file is separated by ','. So, your file should look like:
[{
"Name": "ABC",
"Phone": "123",
"Address": [{
"City": "City-1"
}, {
"Country": "Country-1"
}]
},
{
"Name": "ABC-1",
"Phone": "123-1",
"Address": [{
"City": "City-2"
}, {
"Country": "Country-2"
}]
}
]
You can read the file and output as follows :
import json
my_file='test.json'
with open(my_file, 'r') as my_data:
data = json.load(my_data)
print data
for elm in data:
print elm['Phone']
print elm['Name']
print elm['Address'][0]['City']
print elm['Address'][1]['Country']
First of all this is not valid json format,
You can check here you json format
You should use this try.json file
{
"data":[{
"Name": "ABC",
"Phone":"123",
"Address":[{"City":"City-1"},{"Country":"Country-1"}]
}, {
"Name": "ABC-1",
"Phone":"123-1",
"Address":[{"City":"City-2"},{"Country":"Country-2"}]
}]
}
here is try.py
import json
json_open = open("try.json")
json_data = json.load(json_open)
for i in range(len(json_data['data'])):
print(json_data['data'][i]["Name"])
Note : python code is written in python3
output is look line this
ABC
ABC-1