Ajinkya
Ajinkya

Reputation: 1867

Python nested dictionaries

I made a code to generate json. The code is :

import collections
d = collections.defaultdict(dict)

    with open(res_file, 'w') as f:
        for i, box in enumerate(boxes):


            a=np.split(poly,4)

            d[i][str(0)] = str(a[0])
            d[i][str(1)] = str(a[1])
            d[i][str(2)] = str(a[2])
            d[i][str(3)] = str(a[3])

            y = json.dumps(d)
            print(y)

Its output is

{"0": {"0": "[429 44]", "1": "[436 44]", "2": "[436 56]", "3": "[429 56]"}, "1": {"0": "[345 41]", "1": "[431 44]", "2": "[430 69]", "3": "[344 66]"}, "2": {"0": "[453 42]", "1": "[554 42]", "2": "[554 68]", "3": "[453 68]"}, "3": {"0": "[654 45]", "1": "[710 45]", "2": "[710 76]", "3": "[654 76]"}, "4": {"0": "[436 48]", "1": "[449 48]", "2": "[449 66]", "3": "[436 66]"}, "5": {"0": "[153 58]", "1": "[287 61]", "2": "[286 97]", "3": "[152 93]"}, "6": {"0": "[345 70]", "1": "[438 70]", "2": "[438 94]", "3": "[345 94]"}, "7": {"0": "[442 69]", "1": "[477 69]", "2": "[477 94]", "3": "[442 94]"}, "8": {"0": "[481 69]", "1": "[602 69]", "2": "[602 94]", "3": "[481 94]"}, "9": {"0": "[638 76]", "1": "[724 76]", "2": "[724 94]", "3": "[638 94]"}, "10": {"0": "[293 117]", "1": "[313 117]", "2": "[313 132]", "3": "[293 132]"}, "11": {"0": "[316 117]", "1": "[361 117]", "2": "[361 132]", "3": "[316 132]"}

I want to do this: If all the boxes (in above example 11 boxes) have successfully executed running then append the result with:

"Message":"success",
"Status":1,

My final output json should look like:

{ 
    "Status":1,
    "Message":"success",
    "Result":{ 
        "1":{ 
            "1":"[431,44]",
            "0":"[345,41]",
            "3":"[344,66]",
            "2":"[430,69]"
        },
        "0":{ 
            "1":"[436,44]",
            "0":"[429,44]",
            "3":"[429,56]",
            "2":"[436,56]"
        },
        ...
    }
}

How do I do that?

Upvotes: 1

Views: 102

Answers (2)

Shabbir
Shabbir

Reputation: 153

I am not replicating your entire code.Assuming that your above code works fine to return the data, use the code below your nested dict.

Please have a look at the below code.

#To dump the json output you will require the json package, import it using
import json

#this is the data which you have dumped in your question, assuming that your above code generates this data
data = {'0': {'0': '[429 44]', '1': '[436 44]', '2': '[436 56]', '3': '[429 56]'}, '1': {'0': '[345 41]', '1': '[431 44]', '2': '[430 69]', '3': '[344 66]'}, '2': {'0': '[453 42]', '1': '[554 42]', '2': '[554 68]', '3': '[453 68]'}, '3': {'0': '[654 45]', '1': '[710 45]', '2': '[710 76]', '3': '[654 76]'}, '4': {'0': '[436 48]', '1': '[449 48]', '2': '[449 66]', '3': '[436 66]'}, '5': {'0': '[153 58]', '1': '[287 61]', '2': '[286 97]', '3': '[152 93]'}, '6': {'0': '[345 70]', '1': '[438 70]', '2': '[438 94]', '3': '[345 94]'}, '7': {'0': '[442 69]', '1': '[477 69]', '2': '[477 94]', '3': '[442 94]'}, '8': {'0': '[481 69]', '1': '[602 69]', '2': '[602 94]', '3': '[481 94]'}, '9': {'0': '[638 76]', '1': '[724 76]', '2': '[724 94]', '3': '[638 94]'}, '10': {'0': '[293 117]', '1': '[313 117]', '2': '[313 132]', '3': '[293 132]'}, '11': {'0': '[316 117]', '1': '[361 117]', '2': '[361 132]', '3': '[316 132]'}}

final_data = {
    "Status": 1 if data else 0,
    "Message": "success" if data else "failed",
    "Result": data
}

Use the json package to dump the data as json

d = json.dumps(final_data,indent=4)

print(d)

Hope this helps

Thanks

Upvotes: 0

Olvin Roght
Olvin Roght

Reputation: 7812

You can just create one more dict with required fields:

from collections import defaultdict

d = defaultdict(dict)

with open(res_file, 'w') as f:
    for i, box in enumerate(boxes):
        poly = np.array(box).astype(np.int32).reshape((-1))
        a = np.split(poly, 4)
        for j in range(4):
            d[i][str(j)] = str(a[j])

result = {
    'Status': int(bool(d)),  # 0 - dict empty, 1 - dict not empty
    'Message': 'success' if d else 'fail',  # ('fail', 'success')[int(bool(d))]
    'Result': d
}

If you need to print this dict indented, you can use json.dumps():

import json

print(json.dumps(result, indent=4))

Upvotes: 2

Related Questions