Reputation: 3065
I am trying to produce the following JSON structure via python. This was taken from the mongoDB web site
{
timestamp_hour: ISODate("2013-10-10T23:00:00.000Z"),
type: “memory_used”,
values: {
0: { 0: 999999, 1: 999999, …, 59: 1000000 },
1: { 0: 2000000, 1: 2000000, …, 59: 1000000 },
…,
58: { 0: 1600000, 1: 1200000, …, 59: 1100000 },
59: { 0: 1300000, 1: 1400000, …, 59: 1500000 }
}
}
So far I have the following:
import json
import random
data = {}
data['type'] = "memory_used"
data['timeStamp_Hour'] = "2018-10-01T00:00:00.000Z"
data['values'] = []
for x in range(59):
for y in range(59):
data['values'].append({
x : round(random.uniform(0, 100), 2)
})
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
Which gives me:
{
"type": "memory_used",
"timeStamp_Hour": "2018-10-01T00:00:00.000Z",
"values": [
{
"0": 9.8
},
{
"0": 33.61
},
......
I am not sure how to structure the loop so I can append to the values section.
Upvotes: 0
Views: 677
Reputation: 19685
Looks like the values
is meant to be Dict[int, Dict[int,int]]
rather than a list as you have.
...
data['values'] = {}
for x in range(59):
d={}
for y in range(59):
d[y]=round(random.uniform(0, 100), 2)
data['values'][x]= d
...
Upvotes: 2