Python Trying to read json file and print specific information - python

So, I need to load specific "questions" from a json file, when I loaded it, it just shows this: {'Question1': 'What is your name?'}. How do I make it just show "What is your name"?
My Code:
import json
with open("main.json", "r") as f:
responses = json.load(f)
print(responses)
Json File:
{
"Question1": "What is your name?"
}

import json
with open("main.json", "r") as f:
responses = json.load(f)
print(responses["Question1"])
In this approach all questions from JSON file will be read by json.load(f) instruction and stored within dictionary called responses. Keys of this dict will be question names f.e "Question1" and values will be question contents f.e "What is your name?".
When we have more questions we can iterate over them:
import json
with open("main.json", "r") as f:
responses = json.load(f)
for question_name, question_content in responses.items():
print(question_content)
So for this JSON file:
{
"Question1": "What is your name?",
"Question2": "What is your age?"
}
it will print:
What is your name?
What is your age?

Related

Convert json with data from each id in different lines into one line per id with python

I have a json file with the following format:
{
"responses":[
{
"id":"123",
"cid":"01A",
"response":{nested lists and dictionaries}
},
{
"id":"456",
"cid":"54G",
"response":{nested lists and dictionaries}
}
]}
And so on.
And I want to convert it into a json file like this:
{"id":"123", "cid":"01A", "response":{nested lists and dictionaries}},
{"id":"456", "cid":"54G", "response":{nested lists and dictionaries}}
or
{responses:[
{"id":"123", "cid":"01A", "response":{nested lists and dictionaries}},
{"id":"456", "cid":"54G", "response":{nested lists and dictionaries}}
]}
I don't care about the surrounding format as long as I have the information for each ID in just one line.
I have to do this while reading it because things like pd.read_json don't read this kind of file.
Thanks!
Maybe just dump it line wise? But I guess I didn't understand your question right?
import json
input_lines = {"responses": ...}
with open("output.json", "w") as f:
for line in input_lines["responses"]:
f.write(json.dumps(line) + "\n")
You can use the built-in json library to print each response on a separate line. The json.dump() function has an option to indent, if you want that, but its default is to put everything on one line, like what you want.
Here's an example that works for the input you showed in your post.
#!/usr/bin/env python3
import json
import sys
with open(sys.argv[1]) as json_file:
obj = json.load(json_file)
print("{responses:[")
for response in obj['responses']:
print(json.dumps(response))
print("]}")
Usage (assuming you named the program format_json.py):
$ chmod +x format_json.py
$ format_json.py my_json_input.json > my_json_output.json
Or, if you're not in a command-line environment, you can also hardcode the input and output filenames:
#!/usr/bin/env python3
import json
import sys
infile = 'my_json_input.json'
outfile = 'my_json_output.json'
with open(infile) as json_file:
obj = json.load(json_file)
print("{responses:[", file=outfile)
for response in obj['responses']:
print(json.dumps(response), file=outfile)
print("]}", file=outfile)

How to change the JSON file contents for the below json file

I have a json file say 1.control_file.txt and i need to chnage it's values every now and then,so say i have a ticket Sample1 and with that i need to change the date field, also other fields are required sometimes.
So suppose say i fetched the ticket no as user input and so how do i change the ticket field together with suppose say a given start and end date.
Also the exporter names tag in the json file should be changeable..
Can any on suggest me on how do i do that using shell or python?
Fields i am taking as user input user,ticket,startdate,end_date and sample_names...
"user": "dexter",
"ticket": "Sample1",
"start_date": "2018-07-02",
"end_date": "2019-07-02",
"sample_names": [
"Demo1exp1",
"Demo2exp2",
"Demo3exp3",
"Demo4exp4",
"Demo5exp5",
"Demo6exp6",
"Demo7exp7",
"Demo8exp8",
"Demo9exp9"
]
}```
This snippet of code is probably what you need
import json
with open('data.txt') as json_file:
data = json.load(json_file)
data['start_date'] = "2018-07-03"
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)

How to find and replace a part of a value in json file

I have a json file that I am using as a Dictionary in python.
The json file is really long with 10k+ records. I need to replace the $home part in the "iscategorical" with the value of "id". After making the changes, I want to save this file so that I can use it again as a dictionary. Thank you for the help. Here is a sample:
{
"maps": [
{
"id": "xyzp",
"iscategorical": "/u/$home/app/home"
},
{
"id": "trtn",
"iscategorical": "/u/app/$home/user"
}
]}
I am understanding that you are able to load the file successfully, and all you want to do is replace the strings and save the structure to file again.
For this, we can traverse the list of dictionaries in the data, and modify the value of item['iscategorical'] by replacing $home with the value of item['id'].
We can then dump the modified structure back to (a new) json file.
import json
with open('data.json') as f:
data = json.load(f)
for item in data['maps']:
item['iscategorical'] = item['iscategorical'].replace('$home', item['id'])
with open('new_data.json', 'w') as f:
json.dump(data, f)
Your question seems similar to - Parsing values from a JSON file? .
However for your case below snippet should work.
import json
with open('idata.json') as infile:
data = json.load(infile)
for elem in data["maps"]:
elem['iscategorical']=elem['iscategorical'].replace('$home',elem['id'])
with open('odata.json', 'w') as outfile:
json.dump(data, outfile)
If it's a file, one thing you can do is load the file in and read line by line.
for everyline, you can use regex to find and replace. Then you can either overwrite the file or write onto a new file.
For example,
line.replace('$home', 'id')
Alternatively, you can load the json python in and convert it into a string. Then replace the text using the regex. Finally, converts back to Python dictionary using json.load().
However, 10k line is too long. I think reading a file, line-by-line, would be a better solutions.
EDIT:
Here is the code sample.
from tempfile import mkstemp
from shutil import move
from os import fdopen, remove
def replace(file_path, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
with fdopen(fh,'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
new_file.write(line.replace(pattern, subst))
#Remove original file
remove(file_path)
#Move new file
move(abs_path, file_path)
replace('./text.txt', '$home', 'id')
"The JSON file is really long with 10k+ records" -Try this way it should help for large files.
input.json
{"maps":[{"id":"xyzp","iscategorical":"/u/$home/app/home"},{"id":"trtn","iscategorical":"/u/app/$home/user"}]}
import json
with open('input.json') as f:
data = json.load(f)
my_list = []
def get_some_data():
for item in data['maps']:
yield(item['id'], item['iscategorical'])
for id, iscat in get_some_data():
temp_dict = {}
temp_dict['id'] = id
temp_dict['iscategorical'] = iscat.replace('$home', id)
my_list.append(temp_dict)
maps_dict = {}
maps_dict['maps'] = my_list
with open('output.json', 'w') as f:
json.dump(maps_dict, f)
output.json:
{"maps": [{"id": "xyzp", "iscategorical": "/u/**xyzp**/app/home"}, {"id": "trtn", "iscategorical": "/u/app/**trtn**/user"}]}

Adding values to JSON file in python

I currently have a son file created called "newsuser.json". I am trying to get a users name to be saved to the son file each time a new user is added. I can currently get a user to be saved, but when another user is added the previous user is overwritten, resulting in only having one user stored.
I am trying to get me JSON file to look like this:
{
"users":[
{
"name":"test1"
},
{
"name":"test2"
}
]
}
Instead my output looks like:
{"name": "test1"}
Here is my code for creating a JSON object and saving to "newsuser.json"
import json
userName = form.getvalue('userName')
newDict = {"name": "test1"}
with open("cgi-bin/newsuser.json") as f:
data = json.load(f)
data.update(newDict)
with open("cgi-bin/newsuser.json", "w") as f:
json.dump(data, f)
Does anyone have ideas how I can get a JSON file to print my objects under "user" and not overwrite each entry?
just replace 'w' with 'a' like below
with open("cgi-bin/newsuser.json", "a") as f:
json.dump(data, f)

How to update a JSON file by using Python?

I am using Python and I have a JSON file in which I would like to update a value related to a given key. That is, I have the my_file.json containing the following data
{"a": "1", "b": "2", "c": "3"}
and I would like to just change the value related to the b key from 2 to 9 so that the updated file look as like:
{"a": "1", "b": "9", "c": "3"}
How can I make that?
I tried the following but without success (the changes are not saved to the file):
with open('my_file.json', 'r+') as f:
json_data = json.load(f)
json_data['b'] = "9"
f.close()
You did not save the changed data at all. You have to first load, then modify, and only then save. It is not possible to modify JSON files in-place.
with open('my_file.json', 'r') as f:
json_data = json.load(f)
json_data['b'] = "9"
with open('my_file.json', 'w') as f:
f.write(json.dumps(json_data))
You may also do this:
with open('my_file.json', 'r+') as f:
json_data = json.load(f)
json_data['b'] = "9"
f.seek(0)
f.write(json.dumps(json_data))
f.truncate()
If you want to make it safe, you first write the new data into a temporary file in the same folder, and then rename the temporary file onto the original file. That way you will not lose any data even if something happens in between.
If you come to think of that, JSON data is very difficult to change in-place, as the data length is not fixed, and the changes may be quite significant.
You are almost there, you only have to write the updated json_data back to the file. Get rid of f.close(), as the with statement will ensure that the file is closed. Then, issue
with open('my_file.json', 'w') as f:
f.write(json.dumps(json_data))
This is simplest way to do the json file updation/writing.
where you are creating instance of json file as 'f' and the writing the 'data' into the json file,
#write json file
with open('data.json', 'w') as f:
json.dump(data, f)
#Read json file
with open('data.json', 'r') as f:
json.load(data, f)
Open the file and store in one variable all the content using json.load function
Update your key stored in the previous variable
Open the file another time and update your content with the variable updated
def updateJsonFile():
jsonFile = open("my_file.json", "r") # Open the JSON file for reading
data = json.load(jsonFile) # Read the JSON into the buffer
jsonFile.close() # Close the JSON file
## Working with buffered content
data["b"] = "9"
## Save our changes to JSON file
jsonFile = open("my_file.json", "w+")
jsonFile.write(json.dump(data))
jsonFile.close()

Categories