I need to build the following JSON structure dynamically.
json = {
"mainkey":"val1",
"key2":[
{"keya":"val1rec1","keyb":"val2rec1","keyc":"val3rec1"},
{"keya":"val1rec2","keyb":"val2rec2","keyc":"val3rec2"},
{"keya":"val1rec3","keyb":"val2rec3","keyc":"val3rec3"},
{"keya":"val1rec4","keyb":"val2rec4","keyc":"val3rec4"},
{"keya":"val1rec5","keyb":"val2rec5","keyc":"val3rec5"}
]
}
only the "{"keya":"val1rec1","keyb":"val2rec1","keyc":"val3rec1"}," rows "iterate" - ie, reading values from a CSV file and then populating/creating the rows based on what is inside a CSV file.
So my pseudo code looks something like this:
#create dict
path = 'somewhere\on\my\disk\file.csv'
json_file = {}
json_file['mainkey'] = "val1"
#read from CSV file
df1 = pd.read_csv(path, header=None)
#iterate through csv
for row,s in df1.iterrows():
number = df1.loc[row,0]
#I'm reading keyb and keyc values from CSV as well, but for brevity my substitution below is not showing that....
json_file['key2'] = "'keya':'"+str(number)+"','keyb':'whatever','keyc':'whatever'"
print (json_file)
It obviously fails to produce what I'm looking for above - hence my post here for assistance.
It looks like you're trying to construct a json-encoder manually, this is unecessary since there's a great json-encoder built into python.
I'd recommend building up your dict using the native data structre and use the builtin json-utilites. This will both produce cleaner more maintainable code and is less error prone.
Like this:
import json
# ... Other imports you may have such as pandas
path = "somewhere\on\my\disk\file.csv"
# Initialize dict
data = {"mainkey": "val1", "key2": list()}
# Parse CSV file
df1 = pd.read_csv(path, header=None)
# iterate through csv
for row,s in df1.iterrows():
number = df1.loc[row,0]
# I'm reading keyb and keyc values from CSV as well,
# but for brevity my substitution below is not showing that....
data['key2'].append({
"keya":number,
"keyb":"whatever",
"keyc":"whatever",
})
# Print json to stdout/terminal
json_data = json.dumps(data)
print(json.dumps(data, sort_keys=True))
# Save json to file (data.json)
with open("data.json", "w") as output:
json.dump(data, output, sort_keys=True)
You are overwriting key2 values while you should append them to the list:
json_file['key2'] = []
for row,s in df1.iterrows():
number = df1.loc[row,0]
json_file['key2'].append({'keya': str(number), 'keyb': 'whatever', 'keyc': 'whatever'})
print (json_file)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With