Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json.loads() returns a string

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?

like image 318
Zid Avatar asked Jun 24 '19 11:06

Zid


People also ask

Does JSON load return a string?

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.

What does JSON loads () return?

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.

What is JSON loads () method?

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.

How do I load a JSON string in Python?

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.


4 Answers

Two points:

  1. You have a typo in your products key : "final_price": 40, should be "final_price": 40 (without comma)
  2. 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.

like image 187
Sebastien D Avatar answered Oct 08 '22 05:10

Sebastien D


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.

like image 29
Rahul charan Avatar answered Oct 08 '22 04:10

Rahul charan


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'>
like image 40
bharatk Avatar answered Oct 08 '22 04:10

bharatk


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).

like image 45
holdenweb Avatar answered Oct 08 '22 04:10

holdenweb