How to update values of a dictionary in a json file? - python

I'm trying to add user info to a nested dictionary within a json file. Here's the code.
import json
dictionary = {'name': 'Tony Stark', 'attributes': ['genius', 'billionaire', 'playboy', 'philanthropist']}
with open('info.json', 'a+') as file:
json.dump(dictionary, file)
The info.json file
{'marvel': [
{'name': 'Bill Gates', 'attributes': ['philanthropist', 'programmer']}
]
}
Now, I am unable to dump the dictionary as a value for marvel which is a list. I'm trying to make it dynamic, adding Tony Stark's info to the json file.
Please help me with that, thanks.

Alternative:
import json
dictionary = {'name': 'Tony Stark', 'attributes': ['genius', 'billionaire', 'playboy', 'philanthropist']}
def write_json(data, filename='file.json'):
# function to add to JSON
with open(filename,'w') as f:
json.dump(data, f, indent=4)
with open('file.json') as json_file:
data = json.load(json_file)
data['marvel'].append(dictionary) # appending data to Marvel
write_json(data)
Edited as per observation of juanpa.arrivillaga

Related

How to convert a line in csv to a list of objects

Each line in my csv file is data on a pet. Ex"Fish, Nemo, April 2nd, Goldfish, Orange." I would like to import that file and create a new object for that pet depending on its type(the first string in each line). For example data about the fish would be stored in a fish object. I then want to put each object into a list.
I've tried:
pets = []
with open('desktop/cs110/pets.csv', 'r') as file:
csvReader = csv.reader(file, delimiter=',')
for row_pets in csvReader:
pets.append(row_pets)
columnNames = ['firstCol', 'secondCol', 'thirdColomn']
lstPets = []
for row_pets in pets:
lstPets.append({key: value for key, value in zip(columnNames, row_pets)})
return lstPets
With csv.DictReader you can accomplish what your current code attempts by specifying fieldnames and assuming your "object" desired is a dictionary:
pets.csv
Fish,Nemo,April 2nd,Goldfish,Orange
Cat,Garfield,June 1st,Tabby,Orange
test.py
import csv
from pprint import pprint
with open('pets.csv', newline='') as file:
reader = csv.DictReader(file, fieldnames='type name bday species color'.split())
data = list(reader)
pprint(data)
Output
[{'bday': 'April 2nd',
'color': 'Orange',
'name': 'Nemo',
'species': 'Goldfish',
'type': 'Fish'},
{'bday': 'June 1st',
'color': 'Orange',
'name': 'Garfield',
'species': 'Tabby',
'type': 'Cat'}]

Read JSON-file in Python

I have a json file structured like this:
[
{"ID":"fjhgj","Label":{"objects":[{"featureId":"jhgd","schemaId":"hgkl","title":"Kuh","}],"classifications":[]},"Created By":"xxx_xxx","Project Name":"Tiererkennung"},
{"ID":"jhgh","Label":{"objects":[{"featureId":"jhgd","schemaId":"erzl","title":"Kuh","}],"classifications":[]},"Created By":"xxx_xxx","Project Name":"Tiererkennung"},
...
and I would like to read all IDs and all schemaIds for each entry in the json file. I am codin in python.
What I tried is this:
import json
with open('Tierbilder.json') as f:
data=json.load(f)
data1 =data[0]
print(data1.values)
server_dict = {k:v for d in data for k,v in d.items()}
host_list = server_dict
Now I have the Problem that in host_list only the last row of my json file is saved. How can I get another row, like the first one?
Thanks for your help.
structure your JSON so it's readable and structure is clear
simple list comprehension
data you will have been read from your file
data = [{'ID': 'fjhgj',
'Label': {'objects': [{'featureId': 'jhgd','schemaId': 'hgkl','title': 'Kuh'}], 'classifications': []},
'Created By': 'xxx_xxx','Project Name': 'Tiererkennung'},
{'ID': 'jhgh', 'Label': {'objects': [{'featureId': 'jhgd','schemaId': 'erzl','title': 'Kuh'}], 'classifications': []},
'Created By': 'xxx_xxx','Project Name': 'Tiererkennung'}]
projschema = [{"ID":proj["ID"], "schemaId":schema["schemaId"]}
for proj in data
for schema in proj["Label"]["objects"]]
output
[{'ID': 'fjhgj', 'schemaId': 'hgkl'}, {'ID': 'jhgh', 'schemaId': 'erzl'}]

python append json object to json list within a file

I have file.json that contains
{"list" : [
]}
I have this dict
lead = {'title': i.css('article>.sales-info>h1::text').extract_first(),
'address': i.css('article>.primary-info>.contact>h2::text').extract_first(),
'phone': i.css('article>.primary-info>.contact>.phone::text').extract_first(),
'time': i.css('article>.primary-info>.contact>.time-info>div:nth-child(2)::text').extract_first(),
'website': i.css('.business-card-footer>.website-link::attr(href)').extract_first(),
'email': i.css('.business-card-footer>.email-business::attr(href)').extract_first(),
'portfolio_item': ''}
I need to append this dict to the json list.
I've tried basic writing to file
with open('leads.json', 'a') as f:
f.write(json.dumps(item))
f.close()
Have no idea how should i append it to the json list
I think this could work for you:
EDIT: forgot to add the write_json() function
def write_json(path, data, indent=4):
with open(path, 'w') as file:
json.dump(data, file, indent=indent)
def append_json(path, data, key=None):
if not os.path.exists(path):
write_json(path, data)
else:
with open(path) as file:
# load a dict of the json data
json_data = json.load(file)
# key should be a list of objects so you can __add__ them
if key is None:
json_data += data
else:
json_data[key] += data
write_json(path, json_data)
appen_json('./leads.json', [lead], 'list') # you need to pass in a list, not just dict
The way this is written requires you to pass in a list rather than a dictionary but that has an advantage because you can pass in multiple items to append rather than one-at-a-time
This will write a new file if one doesn't exist in the director OR append if it does exist

Python JSON append if value doesn't exist

I've got a json file with 30-ish, blocks of "dicts" where every block has and ID, like this:
{
"ID": "23926695",
"webpage_url": "https://.com",
"logo_url": null,
"headline": "aewafs",
"application_deadline": "2020-03-31T23:59:59",
}
Since my script pulls information in the same way from an API more than once, I would like to append new "blocks" to the json file only if the ID doesn't already exist in the JSON file.
I've got something like this so far:
import os
check_empty = os.stat('pbdb.json').st_size
if check_empty == 0:
with open('pbdb.json', 'w') as f:
f.write('[\n]') # Writes '[' then linebreaks with '\n' and writes ']'
output = json.load(open("pbdb.json"))
for i in jobs:
output.append({
'ID': job_id,
'Title': jobtitle,
'Employer' : company,
'Employment type' : emptype,
'Fulltime' : tid,
'Deadline' : deadline,
'Link' : webpage
})
with open('pbdb.json', 'w') as job_data_file:
json.dump(output, job_data_file)
but I would like to only do the "output.append" part if the ID doesn't exist in the Json file.
I am not able to complete the code you provided but I added an example to show how you can achieve the none duplicate list of jobs(hopefully it helps):
# suppose `data` is you input data with duplicate ids included
data = [{'id': 1, 'name': 'john'}, {'id': 1, 'name': 'mary'}, {'id': 2, 'name': 'george'}]
# using dictionary comprehension you can eliminate the duplicates and finally get the results by calling the `values` method on dict.
noduplicate = list({itm['id']:itm for itm in data}.values())
with open('pbdb.json', 'w') as job_data_file:
json.dump(noduplicate, job_data_file)
I'll just go with a database guys, thank you for your time, we can close this thread now

Python: converting a list of dictionaries to json

I have a list of dictionaries, looking some thing like this:
list = [{'id': 123, 'data': 'qwerty', 'indices': [1,10]}, {'id': 345, 'data': 'mnbvc', 'indices': [2,11]}]
and so on. There may be more documents in the list. I need to convert these to one JSON document, that can be returned via bottle, and I cannot understand how to do this. Please help. I saw similar questions on this website, but I couldn't understand the solutions there.
use json library
import json
json.dumps(list)
by the way, you might consider changing variable list to another name, list is the builtin function for a list creation, you may get some unexpected behaviours or some buggy code if you don't change the variable name.
import json
list = [{'id': 123, 'data': 'qwerty', 'indices': [1,10]}, {'id': 345, 'data': 'mnbvc', 'indices': [2,11]}]
Write to json File:
with open('/home/ubuntu/test.json', 'w') as fout:
json.dump(list , fout)
Read Json file:
with open(r"/home/ubuntu/test.json", "r") as read_file:
data = json.load(read_file)
print(data)
#list = [{'id': 123, 'data': 'qwerty', 'indices': [1,10]}, {'id': 345, 'data': 'mnbvc', 'indices': [2,11]}]
response_json = ("{ \"response_json\":" + str(list_of_dict)+ "}").replace("\'","\"")
response_json = json.dumps(response_json)
response_json = json.loads(response_json)
To convert it to a single dictionary with some decided keys value, you can use the code below.
data = ListOfDict.copy()
PrecedingText = "Obs_"
ListOfDictAsDict = {}
for i in range(len(data)):
ListOfDictAsDict[PrecedingText + str(i)] = data[i]

Categories

Resources