-1

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."
  • that is only the python representation, you should not worry about that. What is the problem that you have? – Netwave May 03 '19 at 08:13
  • write it in file - you don't have to change quotes. – furas May 03 '19 at 08:14
  • ill print some hundreds of random data and post somewhere else. But it ll accept only " " not ' ' –  May 03 '19 at 08:15
  • it doesn't matter - `print()` use single quota to show it on screen but module `json` will write it with double quota. – furas May 03 '19 at 08:16
  • tried in jupyter same ' ' are returning –  May 03 '19 at 08:17
  • you don't understand - module `json` will use correct double quota. – furas May 03 '19 at 08:18
  • besides, quota is not part of string it is only added when you use `print()` to display list or dictionary. It adds quota to show that you have string `'123'` or number `123` – furas May 03 '19 at 08:21
  • avoid answers with .REPLACE!! use json library, json.dumps(dict) to output, and load/loads to input – Wonka May 03 '19 at 08:33

3 Answers3

3

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.

VPfB
  • 14,927
  • 6
  • 41
  • 75
0

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"}
furas
  • 134,197
  • 12
  • 106
  • 148
-2

you can use:

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