The following code generates random json data:
import json
my_dict = {'foo': 42, 'bar': {'baz': "Hello", 'poo': 124.2}}
my_json = json.dumps(my_dict)
print(my_json)
How to automate it so that it generates random json data everytime. Know that faker library in python does generate random data. How to combine it with json.
import json
from faker import Faker
import random
from random import randint
fake = Faker('en_US')
for _ in range(10):
my_dict = { 'foo': randint(0, 100), 'bar': {'baz': fake.name(), 'poo': float(random.randrange(155, 389))/100 } }
print(my_dict)
Output:
{'foo': 19, 'bar': {'baz': 'Kimberly Robinson', 'poo': 3.33}}
{'foo': 64, 'bar': {'baz': 'Brandon Goodwin', 'poo': 3.55}}
{'foo': 48, 'bar': {'baz': 'David Jacobs', 'poo': 1.74}}
{'foo': 20, 'bar': {'baz': 'John Stout', 'poo': 2.43}}
{'foo': 68, 'bar': {'baz': 'Janet Adams', 'poo': 2.48}}
{'foo': 57, 'bar': {'baz': 'Amy Odonnell', 'poo': 3.34}}
{'foo': 49, 'bar': {'baz': 'Steven Greene', 'poo': 2.34}}
{'foo': 7, 'bar': {'baz': 'Stephen Robinson', 'poo': 1.93}}
{'foo': 32, 'bar': {'baz': 'Joel Wise', 'poo': 3.37}}
{'foo': 33, 'bar': {'baz': 'Stephanie Thompson DDS', 'poo': 2.41}}