使用 Python 创建复杂的 JSON 并创建一些有条件的嵌套数组
Using Python to Create Complex JSON with the Creation of Some Nested Arrays being Conditional
在使用 Python 动态创建较大的 JSON 字符串的一部分时,我想添加一个对象(包含列表和字典的嵌套字典,或者在转换为 [=36 时最终包含对象和数组=]) 仅当更高级别的值(在我的例子中 table 名称)是特定值时。
目前的结构是一个 TNFL,我想有条件地向结构添加另一个对象以及 "fields" & "Values"。
我目前拥有的:
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
当table'k'等于'table_x'时,我需要最里面的.append/for循环来添加另一个对象/值除了"fields"和"values" 名为 'nestedTableInsert" 的对象有它自己的 .append 函数,它会在我的最终 JSON 中为特定的 table 创建另一个层,这样它看起来像这样但语法正确:
我想做的工作:
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
随着添加的 "nestedTableInsert": 对象的结构与其父插入对象相同,因此最终的 JSON 看起来像(特别是 'nestedTableInserts' 用于 ea 唯一名称):
[{
"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)"
]
}]
}
]
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)
90% 然而,正在创建的嵌套 table 插入的字段和值数组正在填充相同的记录信息,而不是创建与每个父 place_nm 插入关联的唯一 nestedTableInsert 记录信息。仍然需要弄清楚最内层的迭代。
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
在使用 Python 动态创建较大的 JSON 字符串的一部分时,我想添加一个对象(包含列表和字典的嵌套字典,或者在转换为 [=36 时最终包含对象和数组=]) 仅当更高级别的值(在我的例子中 table 名称)是特定值时。
目前的结构是一个 TNFL,我想有条件地向结构添加另一个对象以及 "fields" & "Values"。
我目前拥有的:
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
当table'k'等于'table_x'时,我需要最里面的.append/for循环来添加另一个对象/值除了"fields"和"values" 名为 'nestedTableInsert" 的对象有它自己的 .append 函数,它会在我的最终 JSON 中为特定的 table 创建另一个层,这样它看起来像这样但语法正确:
我想做的工作:
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
随着添加的 "nestedTableInsert": 对象的结构与其父插入对象相同,因此最终的 JSON 看起来像(特别是 'nestedTableInserts' 用于 ea 唯一名称):
[{
"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)"
]
}]
}
]
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)
90% 然而,正在创建的嵌套 table 插入的字段和值数组正在填充相同的记录信息,而不是创建与每个父 place_nm 插入关联的唯一 nestedTableInsert 记录信息。仍然需要弄清楚最内层的迭代。
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