Why is json.loads() returning a string? Here's is my code:
import json
d = """{
"reference": "123432",
"business_date": "2019-06-18",
"final_price": 40,
"products": [
{
"quantity": 4,
"original_price": 10,
"final_price": 40,
}
]
}"""
j = json.loads(json.dumps(d))
print(type(j))
Output:
<class 'str'>
Shouldn't it returning a json object? What change is required here?
json. load() takes a file object and returns the json object. A JSON object contains data in the form of key/value pair. The keys are strings and the values are the JSON types.
load or json. loads() method, it returns a Python dictionary. If you want to convert JSON into a custom Python object then we can write a custom JSON decoder and pass it to the json.
loads() json. loads() method can be used to parse a valid JSON string and convert it into a Python Dictionary. It is mainly used for deserializing native string, byte, or byte array which consists of JSON data into Python Dictionary.
Use the json. loads() function. The json. loads() function accepts as input a valid string and converts it to a Python dictionary. This process is called deserialization – the act of converting a string to an object.
Two points:
products
key : "final_price": 40,
should be "final_price": 40
(without comma)j
should be json.loads(d)
Output
dict
EDIT
Reasons why you can not have a trailing comma in a json objects are explained in this post Can you use a trailing comma in a JSON object?
Unfortunately the JSON specification does not allow a trailing comma. There are a few browsers that will allow it, but generally you need to worry about all browsers.
1). The type of d
AND j
will remain same.
import json
d = """{
"reference": "123432",
"business_date": "2019-06-18",
"final_price": 40,
"products": [
{
"quantity": 4,
"original_price": 10,
"final_price": 40,
}
]
}"""
print(type(d))
j = json.loads(json.dumps(d))
print(type(j))
2). Now Both have Dictionary type:-
import json
d = {
"reference": "123432",
"business_date": "2019-06-18",
"final_price": 40,
"products": [
{
"quantity": 4,
"original_price": 10,
"final_price": 40,
}
]
}
print(type(d))
j = json.loads(json.dumps(d))
print(type(j))
This is the reason we use json
format.
I hope this may help you.
ast.literal_eval: Safely evaluate an expression node or a string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, None, bytes and sets. more details
import ast
d = """{
"reference": "123432",
"business_date": "2019-06-18",
"final_price": 40,
"products": [
{
"quantity": 4,
"original_price": 10,
"final_price": 40,
}
]
}"""
data = ast.literal_eval(d)
print(data)
print(type(data))
O/P:
{'reference': '123432', 'business_date': '2019-06-18', 'final_price': 40, 'products': [{'quantity': 4, 'original_price': 10, 'final_price': 40}]}
<class 'dict'>
In your code, d
is supposed to be a JSON string. If it were, you wouldn't therefore need to dump it before loading it.
When I remove the string quotes, meaning that the json.dumps
call is working on a dict not a string everything seems to come out fine:
import json
d = {
"reference": "123432",
"business_date": "2019-06-18",
"final_price": 40,
"products": [
{
"quantity": 4,
"original_price": 10,
"final_price": 40,
}
]
}
j = json.loads(json.dumps(d))
print(type(j))
prints out
<class 'dict'>
Note, however, that trying to apply json.loads
to the existing string will produce an error because JSON is less forgiving than Python, and doesn't allow dangling commas at the end of lists and dicts (see the "final_price"
element definition).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With