Zid
Zid

Reputation: 449

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?

Upvotes: 10

Views: 18250

Answers (4)

bharatk
bharatk

Reputation: 4315

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'>

Upvotes: 3

Sebastien D
Sebastien D

Reputation: 4482

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.

Upvotes: 6

Rahul charan
Rahul charan

Reputation: 837

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.

Upvotes: 3

holdenweb
holdenweb

Reputation: 37003

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

Upvotes: 1

Related Questions