Skip to content Skip to sidebar Skip to footer

Re-nesting A Json Output In Python

I have a json output in a loop and at the end of the loop I need to assign a unique key to the json. Something like the following : out = {'34267':{'Annual Expenditure':250,'Annual

Solution 1:

One possible solution is create nested dictionary and then convert to json by this:

d = df.set_index('Guest').to_dict(orient='index')
d_final = {32: j}

import json
withopen('result.json', 'w') as fp:
    json.dump(d, fp)

Solution 2:

You could simply escape { with {{ as this:

out_final = '{{32:{}}}'.format(out)

Post a Comment for "Re-nesting A Json Output In Python"