简体   繁体   中英

how to add double quotes while printing in json? Currently i am getting single quotes

code

import json 
from faker import Faker
import random
from random import randint
fake = Faker('en_US')
for _ in range(1):
    sds =  {
      "id": "AB-asdfaf",
      "body": fake.sentence(),
      "time": fake.ean(),
      "hash": fake.ean(),
      "id1": fake.ean(),
      "user_id": "asdasdas",
      "id3": "test1"
    }

    print(sds)

output i am getting is in single quotes ''. i need to get the json ijn double quotes

output :

 'body': 'Throughout third tough will PM time treat.'

output i need :

"body": "Throughout third tough will PM time treat."

If you expect correct JSON output, you must convert your data explicitly:

 print(json.dumps(sds))

It is not only about quotes, False , True and None become false , true and null in JSON.

Quota is not part of string and print() adds single quota only to show that you have string '123' or number 123

If you use module json then you get correctly formated JSON with double quota

print( json.dumps(sds) )

Full code

import json 
from faker import Faker
import random
from random import randint
fake = Faker('en_US')
for _ in range(1):
    sds =  {
      "id": "AB-asdfaf",
      "body": fake.sentence(),
      "time": fake.ean(),
      "hash": fake.ean(),
      "id1": fake.ean(),
      "user_id": "asdasdas",
      "id3": "test1"
    }

    print(json.dumps(sds))

Result:

{"id": "AB-asdfaf", "body": "Buy government couple.", "time": "6066358112738", "hash": "1204203048039", "id1": "0212772145043", "user_id": "asdasdas", "id3": "test1"}

您可以使用:

replace('\'', '\"'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM