I'm trying to parse this json file that I recieved from an API call.
"[{\"ip\":\"xx.xx.xxx.xx\",\"dns\":\"xxx.net\",\"netbios\":\"xxxxx\",....
I dumped it to a file like so:
with open('jayo.json', 'w') as j:
json.dump(r.text, j) #r.text being the API response
json should just be a straightforward dictionary, right? why does mine have all the back-slashes?
How would I print each value on it's own? IP/DNS etc.
You are receiving the API response as a str you need to load it using json before dumping it. json.dump is usually used with collections not strings as it does the conversion for you.
data = json.loads(r.text)
with open('jayo.json', 'w') as j:
json.dump(data, j)
If you need the data in the file before overwriting it load it use
with open('jayo.json', 'r') as j:
data = json.load(j)
Are you trying to load the JSON in Python, or dump it to a file? (or both?)
json.dump is for writing a Python object to a JSON file. r.text is just a string, so the resulting format will look like a single string in JSON (including all of the escaped quotes) instead of a full object.
Presumably you want to use json.loads to load the JSON string into a Python object before using json.dump. Or if you want to dump the JSON string straight to a file, you can just use j.write(r.text).
Related
I am using Python to convert data from a xml file to json and putting it in a file. I am using xmltodict to convert to dictionary using 'parse' and then converting into json using 'dumps'. Here is the code below: -
import xmltodict
import json
with open('note.xml') as xml_file:
my_dict=xmltodict.parse(xml_file.read())
xml_file.close()
json_data=json.dumps(my_dict)
with open('note.json', 'w') as f:
json.dump(json_data, f)
Here is a sample xml file that I have used. However, in the output, I am getting something not quite json like, with the added backslash. Looks like gibberish: -
"{\"note\": {\"to\": \"Tove\", \"from\": \"Jani\", \"heading\": \"Reminder\", \"body\": \"Don't forget me this weekend!\"}}"
I am trying to understand why I am not able to get data in proper json form. Is there anything wrong with my code? Please note that I am not a very skilled programmer and have only used Python sporadically.
You need to add below code after json_data=json.dumps(my_dict) to convert string to json object
json_data = json.loads(json_data)
I'm trying to manipulate a JSON file and dump it back out.
Below is the JSON file - Note how there are two dictionaries wrapped together below...:
{"installed":
{"client_id":"xxx",
"project_id":"quickstart-1557411376659",
"auth_uri":"xxx",
"token_uri":"xxx",
"auth_provider_x509_cert_url":"xxx",
"client_secret":"xxx",
"redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]
}
}
I'm trying to use the following python code to read in the JSON file manipulate it, and write it back out.
with open('google_sheets_credentials.json', 'r+') as file:
google_sheets_auth_dict = json.load(file)
#Manipulate file contents here
with open('google_sheets_credentials.json', 'r+') as file:
json.dump(google_sheets_auth_dict, file)
This code fails after a couple of runs because multiple dictionaries need to be wrapped in a list to be written out as JSON - like so:
The reasoning behind this requirement is explained here
with open('google_sheets_credentials.json', 'r+') as file:
json.dump([google_sheets_auth_dict], file)
The problem is that I can't do that in this case because this JSON is ultimately being fed into google sheets' API where it does not expect the JSON to be wrapped in a list.
How might I read this file in, manipulate, and spit it back out in the format expected by google?
This example case:
import json
example_json = '''{"installed":
{"client_id":"xxx",
"project_id":"quickstart-1557411376659",
"auth_uri":"xxx",
"token_uri":"xxx",
"auth_provider_x509_cert_url":"xxx",
"client_secret":"xxx",
"redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]
}
}'''
google_sheets_auth_dict = json.loads(example_json)
google_sheets_auth_dict['client_id'] = 'yyy'
print(json.dumps(google_sheets_auth_dict))
seems to work fine
I'm guessing that something is going wrong in the #Manipulate file contents here bit, which is not shown. Or, the example JSON does not look like the failure case.
I'm using python 3.x. I have a python dictionary
my_dict={'key1':'A12b','Key2':'cd12'}
I want to convert it in JSON format. I need it in this format
my_dict1={"key1":"A12b","Key2":"cd12"}
I tried my_dict1=json.dumps(my_dict) & I'm getting '{"key1": "A12b", "Key2": "cd12"}' which is of type string. Can you suggest me how do I convert?
That is as close to JSON as you can get in Python. Send that object to a webserver and it will be interpreted as JSON which is probably what you want.
dict in python is a kind of data structure, while json is a kind of data format;
I suppose you want to convert a dict into a json-formated file? Then you should use json.dump(obj, fp) instead of json.dumps(obj). The first one dumps a python dict obj to a file fp, and the second function make obj into a json formatted string.
In your case, to dump a dict obj into a file example.json, code it like below:
import json
my_dict={'key1':'A12b','Key2':'cd12'}
with open('example.json', 'w') as file:
json.dump(my_dict, file)
Good luck!
I have a JSON file (stored in database.txt) I want to modify using python dictionary in method addEvent():
def addEvent(eventName, start, end, place):
newdict={} #make a new dictionnary
newdict["NAME"]=eventName
newdict["START"]=start
newdict["END"]=end
newdict["place"]=place
try:
with open("database.txt",'r') as file:
content=file.read()
dict=json.loads(content) #make dictionnary with original JSON file
liste.append(newdict)
dico["event"]=liste #combine 2dictionnaries
with open("database.txt", 'w') as file:
file.write(str(dico)) #save my new JSON file
except:
...
My problem:
I can run this method only one time, second time I receive an error message:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
addEvent() method modify my database.txt file: it doesn't incule double quotes anymore but accents so i can't use dict=json.loads(content) second time
My question Did I correctly save my JSON file ? How can I keep my JSON format in my database.txt file (keep double quotes) ?
You produced Python syntax by converting the object with str(); Python strings can use either single or double quotes. To produce a JSON string, use json.dumps():
file.write(json.dumps(dico))
The json module has variants of json.loads() and json.dumps() that work directly with files; there is no need to read or write yourself, just use the function names without the trailing s on file objects directly:
with open("database.txt", 'r') as file:
d = json.load(file)
and
with open("database.txt", 'w') as file:
json.dump(dico, file)
The problem is here:
file.write(str(dico)) #save my new JSON file
Use json.dumps instead:
file.write(json.dumps(dico)) #save my new JSON file
I am attempting to parse this JSON file. I need to be able to access and preform a download using the contents of each hash entry. How can I do this in Python without having to manually write code to access each and every entry?
You'd use the json library to parse the data.
You must first load the data from the web and decode it to a Unicode string:
import json
from urllib.request import urlopen
response = urlopen('https://s3.amazonaws.com/Minecraft.Download/indexes/legacy.json')
# the default encoding for JSON is UTF-8, but the response can give you
# a different codec
encoding = response.info().get_content_charset('utf-8')
data = json.loads(response.read().decode(encoding))
Now you can loop through the data:
for name, info in data['objects'].items():
print('{}: {}'.format(name, info['hash']))
This produces:
lang/fr_CA.lang: 6df06576e677d952dc15da3926f4ed822d106354
sounds/random/orb.ogg: e9833a1512b57bcf88ac4fdcc8df4e5a7e9d701d
sounds/mob/villager/yes1.ogg: be73a79fb1ab966887a8b88bed9aa6b3d9173d71
sounds/mob/cat/purreow2.ogg: 08573a1f11058b09c5855122dff47ceb209f771e
sound/mob/spider/say2.ogg: 501b40b97ee55cb7a97943ee620aa05131089fc2
lang/el_GR.lang: 4330e3218548e9f3268d103e5ab71fa2b28d9b20
sound/mob/horse/soft1.ogg: a9a83e3f186b3af56a9844b8d4976c6251ba17eb
sounds/mob/bat/death.ogg: 6df3b2c2b951863fc5200edf3ff3396b105ed897
# etc.
I guess the short answer would be the json module.