NielsNL4
NielsNL4

Reputation: 640

How can i append a dict to a json file in a for loop?

I am making a json file based on an image and in that json file, it needs the information for all the panels within a picture. Because the panel amount is always different I need to append the panel info to the json within a for loop with the range of the amount of panels in the picture but when I do this it only shows the last append in the json file.

Right now I'm using 2 dicts, 1 for the basic json layout thats always needed and another within the for loop for the panels. I am using dict.update(paneldict) inside the for loop but this overwrites itself every cycle


appDict = {
  'type': 'setOfInformationpoints',
  'version': '0.1',
  'informationpoints': [{
                'key': 'worktop01',
                'datatype': 'productmodel:worktop',
                'value': {
                        'type': 'productmodel',
                        'version': '1.2',
                        'globalmeasurments':{
                                'distx': 2777,
                                'disty': 1900,
                                'distz': 60
                        },

                        "panels": [{

                                   }]
                        }
                    }]
                }

def makepanel(index):
    panel = {"id": "panel{}".format(index),
                "measurements": {
                    "distx": 2177,
                    "disty": 600,
                    "distz": 60
                },
                "orientation": "north",
                "position": {
                    "x":0,
                    "y":0
                }
            }
    return panel

for panels in range(2):

    panel = makepanel(panels)
    panel1 = panel.copy()
    appDict.update(panel)
    print(panels)
    with open('data.json', 'w', encoding='utf-8') as outfile:
        json.dump(appDict, outfile, ensure_ascii=False, indent=4)

in this case i have a for loop with a range of 2 so i should have 2 panels in my json: panel0 and panel1 but the actual output is this

{
    "type": "setOfInformationpoints",
    "version": "0.1",
    "informationpoints": [
        {
            "key": "worktop01",
            "datatype": "productmodel:worktop",
            "value": {
                "type": "productmodel",
                "version": "1.2",
                "globalmeasurments": {
                    "distx": 2777,
                    "disty": 1900,
                    "distz": 60
                },
                "panels": [
                    {}
                ]
            }
        }
    ],
    "id": "panel1",
    "measurements": {
        "distx": 2177,
        "disty": 600,
        "distz": 60
    },
    "orientation": "north",
    "position": {
        "x": 0,
        "y": 0
    }
}

and i want it to be like this:

{
    "type": "setOfInformationpoints",
    "version": "0.1",
    "informationpoints": [
        {
            "key": "worktop01",
            "datatype": "productmodel:worktop",
            "value": {
                "type": "productmodel",
                "version": "1.2",
                "globalmeasurments": {
                    "distx": 2777,
                    "disty": 1900,
                    "distz": 60
                },
                "panels": [
                    {}
                ]
            }
        }
    ],
    "id": "panel0",
    "measurements": {
        "distx": 2177,
        "disty": 600,
        "distz": 60
    },
    "orientation": "north",
    "position": {
        "x": 0,
        "y": 0
    }
},
    "id": "panel1",
    "measurements": {
        "distx": 2177,
        "disty": 600,
        "distz": 60
    },
    "orientation": "north",
    "position": {
        "x": 0,
        "y": 0
    }
}

don't mind the extremely bad indentation this is just till i get this to work.

Upvotes: 2

Views: 202

Answers (3)

oaixnah
oaixnah

Reputation: 44

It is best to think of a dictionary as a set of key: value pairs, with the requirement that the keys are unique (within one dictionary)

5.5. Dictionaries -- from https://docs.python.org

Upvotes: 0

André Müller
André Müller

Reputation: 117

If I understand you right you want to merge the second dictionary into the first one without overwriting existing sub-entries. Probably you can solve your problem with the recursive dictionary merge strategy described in Dictionaries of dictionaries merge.

Upvotes: 2

furas
furas

Reputation: 142641

If you want to keep many panels then you have to keep them on list

  [ {"id": "panel0", ... }, {"id": "panel1", ...} ]

or use uniqe key for every panel. Value from id could be the key - "panel0", "panel1", etc.

  { "panel0":{"id": "panel0", ... }, "panel1":{"id": "panel1", ...} ]

Upvotes: 3

Related Questions