json.dumps() converts a dictionary to str object, not a json(dict) object! So you have to load your str into a dict to use it by using json.loads() method
See json.dumps() as a save method and json.loads() as a retrieve method.
This is the code sample which might help you understand it more:
import json
r = {'is_claimed': 'True', 'rating': 3.5}
r = json.dumps(r)
loaded_r = json.loads(r)
loaded_r['rating'] #Output 3.5
type(r) #Output str
type(loaded_r) #Output dict
Answer from Iman Mirzadeh on Stack Overflowjson.dumps() converts a dictionary to str object, not a json(dict) object! So you have to load your str into a dict to use it by using json.loads() method
See json.dumps() as a save method and json.loads() as a retrieve method.
This is the code sample which might help you understand it more:
import json
r = {'is_claimed': 'True', 'rating': 3.5}
r = json.dumps(r)
loaded_r = json.loads(r)
loaded_r['rating'] #Output 3.5
type(r) #Output str
type(loaded_r) #Output dict
json.dumps() returns the JSON string representation of the python dict. See the docs
You can't do r['rating'] because r is a string, not a dict anymore
Perhaps you meant something like
r = {'is_claimed': 'True', 'rating': 3.5}
json = json.dumps(r) # note i gave it a different name
file.write(str(r['rating']))
How to append an dictionary into a json file?
Python create dictionary from json values - Stack Overflow
Convert JSON array to Dictionary
Can I create a TypedDict from a JSON dict?
Hello I currently learning json in python i want to append a dictionary in a json file ontop of existing ones but every time i do this i get this error in VS-Code:
End of file expected.
Can somebody help me?
Here is the Code:
dict = {
"data1" : data3,
"data2" : data4
}
data = json.dumps(dict)
with open("index.json" , "a") as file:
json.dump(data , file)
You may get the values and make dictionary out of it ,
This is what you may do
Code
d = data['response']['globalstats']['heist_success']['history']
result_dict = dict((i["date"],i["total"]) for i in d)
You may also use dict comprehension if using python version 2.7 or above
result_dict = {i["date"]:i["total"] for i in d}
Output
{1486252800: '696574',
1486339200: '357344',
1486425600: '356800',
1486512000: '311056'}
You can use a dict comprehension:
D = {h['date']:h['total'] for h in data['response']['globalstats']['heist_success']['history']}
The for will iterate over the list of dicts in history and date and total are selected as the key and value.