1

In using Python to dynamically create part of a larger JSON string, I want to add an object (nested dictionary containing list and dictionaries or eventually objects and arrays when converted to JSON) only when the higher level value (in my case table name) is a specific value.

The structure so far is a TNFL and I want to conditionally add another object to the structure along with "fields" & "Values".

What I have so far:

constJSON = []
i = 0
for k, v in datDictNorm.iteritems():
    constJSON.append({"table":k, "inserts":[]})
    if v:
        for d in v:
            flds = list(d.keys())
            constJSON[i]["inserts"].append({
                              "fields": flds,
                              "values": [d[f] for f in flds]
            })
        i += 1

when table 'k' equals 'table_x', I need the inner most .append / for loop to add another object / value in addition to "fields" and "values" object named 'nestedTableInsert" that has it's own .append function that would create another layer in my final JSON for only a specific table so that it would look something like this but with correct syntax:

What I am trying to make work:

constJSON = []
i = 0
for k, v in datDictNorm.iteritems():
    constJSON.append({"table": k, "inserts": []})
    if v:
        for d in v:
                flds = list(d.keys())
        if k != "name":
            constJSON[i]["inserts"].append({
                    "fields": flds,
                    "values":  [d[f] for f in flds]})
        else:
            for k2, v2 in prvDictNorm.iteritems():
                constJSON[i]["inserts"].append({
                    "fields": flds,
                    "values":  [d[f] for f in flds],
                    "nestedTableInsert": []})

        i += 1

With the added "nestedTableInsert": object structured the same as it's parent insert object so the final JSON would look like (specifically the 'nestedTableInserts' for ea unique name):

[{
            "table": "place",
            "inserts": [{
                "fields": [
                    "id",
                    "alt_id"
                ],
                "values": [
                    1,
                    1
                ]
            }]
        },
        {
            "table": "data_source",
            "inserts": [{
                "fields": [
                    "id",
                    "col_nm_1",
                    "col_val_1",
                    "valid_from_date",
                    "valid_to_date"
                ],
                "values": [
                    1,
                    "xyz",
                    "1234",
                    "2019-04-16T00:00:00.000Z",
                    "2020-04-16T00:00:00.000Z"
                ]
            }]
        },
        {
            "table": "type",
            "inserts": [{
                "fields": [
                    "id",
                    "alt_id",
                    "type_id",
                    "some_num"
                ],
                "values": [
                    2,
                    1,
                    1,
                    1
                ]
            }]
        },
        {
            "table": "name",
            "inserts": [{
                    "fields": [
                        "some_num",
                        "some_id",
                        "some_other_id",
                        "name"
                    ],
                    "values": [
                        2,
                        1,
                        1,
                        "Minnie Mouse Town"
                    ],
                    "nestedTableInsert": {
                        "table": "prv_feat_nm_li",
                        "inserts": [{
                            "fields": [
                                "id",
                                "col_nm_1",
                                "col_val_1",
                                "nm_type",
                                "nm_ns",
                                "sys_rank",
                                "user_rank",
                                "some_abbr",
                                "some_info",
                                "valid_from_date",
                                "valid_to_date"
                            ],
                            "values": [
                                1,
                                "xyz",
                                "12345",
                                "C",
                                "Minnie Mouse Town",
                                "1",
                                "1",
                                "Q",
                                "Maybe some info here.",
                                "2019-04-16T00:00:00.000Z",
                                "2020-04-16T00:00:00.000Z"
                            ]
                        }]
                    }
                },
                {
                    "fields": [
                        "some_num",
                        "some_id",
                        "some_other_id",
                        "name"
                    ],
                    "values": [
                        2,
                        1,
                        1,
                        "Mickey Mouse Town"
                    ],
                    "nestedTableInsert": {
                        "table": "prv_feat_nm_li",
                        "inserts": [{
                            "fields": [
                                "id",
                                "col_nm_1",
                                "col_val_1",
                                "nm_type",
                                "nm_ns",
                                "sys_rank",
                                "user_rank",
                                "some_abbr",
                                "some_info",
                                "valid_from_date",
                                "valid_to_date"
                            ],
                            "values": [
                                1,
                                "uni",
                                "12346",
                                "C",
                                "Mickey Mouse Town",
                                "1",
                                "1",
                                "Z",
                                "Maybe some info here.",
                                "2019-04-16T00:00:00.000Z",
                                "2020-04-16T00:00:00.000Z"
                            ]
                        }]
                    }
                }
            ]
        },
        {
            "table": "geometry",
            "inserts": [{
                "fields": [
                    "id",
                    "some_other_id",
                    "created",
                    "longitude",
                    "latitude",
                    "shape"
                ],
                "values": [
                    1,
                    1,
                    "No",
                    55.5555555,
                    8.8888888,
                    "POINT(55.5555555 8.8888888)"
                ]
            }]
        }
    ]
kilshaw
  • 21
  • 4

2 Answers2

1
constJSON = []
i = 0
for k, v in datDictNorm.iteritems():
    constJSON.append({"table": k, "inserts": []})
    if v:
        for d in v:
            flds = list(d.keys())
            constJSON[i]["inserts"].append({
                "fields": flds,
                "values": [d[f] for f in flds]
            })
            if k == "table_x":
                constJSON[i]["nestedTableInsert"].append({
                    "fields": flds2,
                    "values": [d2[f2] for f2 in flds2 if k in thing]
                })
        i += 1

myJSON = json.dumps(constJSON)
Alex Hall
  • 31,431
  • 4
  • 39
  • 71
  • This is close to the mark I suspect. One important thing I failed to mention is that the data used to populate the nestedTableInsert field & value [arrays] is being called from a separate dictionary than what is being used to construct the main structure so I need to do two source.iteritems() calls. – kilshaw Apr 22 '19 at 20:20
1

90% there with this however the nested table insert's fields and values arrays being created are populating with the same record information and not creating unique nestedTableInsert record information associated with each parent place_nm insert. Still need to figure out the iteration on this inner most layer.

tableInsert = []
i = 0
for k, v in mainDictNorm.iteritems():
    tableInsert.append({"table": k, "inserts": []})
    if v:
        for d in v:
            flds = list(d.keys())
            if k != "place_nm":
                tableInsert[i]["inserts"].append({
                    "fields": flds,
                    "values":  [d[f] for f in flds]})
            else:
                i = 0
                nestedTableInsert = []
                for k2, v2 in nestDictNorm.iteritems():
                    nestedTableInsert.append({"table": k2, "inserts": []})
                    if v2:
                        for d2 in v2:
                            flds2 = list(d2.keys())
                    tableInsert[i],nestedTableInsert[i]["inserts"].append({
                        "fields": flds2,
                        "values":  [d2[f2] for f2 in flds2]})
                    i += 1
                tableInsert[i]["inserts"].append({
                    "fields": flds,
                    "values":  [d[f] for f in flds],
                    "nestedTableInsert": nestedTableInsert})
        i += 1
kilshaw
  • 21
  • 4