jeudi 19 septembre 2019

Generate Random Json data using Python and store in a varaible

My app takes JSON in this format, How to use for loop to generate data in the mentioned format below and store it in a variable

example :

id='123456'



{
    "abo": [

        {
            "id": "1ab",
            "data": fake.text(),
            "place": fake.place(),
            "user_id": id,
        },

        {
            "id": "1ab",
            "data": fake.text(),
            "place": fake.place(),
            "user_id": id,
        }
    ],
    "id": id
}


How to generate random data for this format and store in a variable by giving range .

So if

range is given as 3 (for i in range(3)) it should dump the following.



{
    "abo": [

        {
            "id": "1ab",
            "data": this has been a great day,
            "place": texas,
            "user_id": '123456',
        },
        {
            "id": "1ab",
            "data": Gaurd should be credited,
            "place": newyork,
            "user_id": '123456',
        },
        {
            "id": "1ab",
            "data": fake.text(),
            "place": fake.place(),
            "user_id": '123456',
        }
    ],
    "id": '123456'
}

Tried this , But this isn't correct way to use json:

import json 
from faker import Faker
import random
from random import randint
print('{"abo": [')
fake = Faker('en_US')
for _ in range(20):
       data=
       {
            "id": "1ab",
            "data": fake.text(),
            "place": fake.place(),
            "user_id": id,
        }
    print(",")
    print(json.dumps(abo))

print('],"id": id}')






Aucun commentaire:

Enregistrer un commentaire