gpt4 book ai didi

python - 使用 Python 创建复杂的 JSON,并根据条件创建一些嵌套数组

转载 作者:行者123 更新时间:2023-12-01 01:00:51 24 4
gpt4 key购买 nike

在使用Python动态创建较大JSON字符串的一部分时,我想仅在更高级别的值(在我的案例表中)时添加一个对象(包含列表和字典的嵌套字典或最终转换为JSON时的对象和数组)名称)是一个特定值。

到目前为止的结构是 TNFL,我想有条件地向该结构添加另一个对象以及“字段”和“值”。

到目前为止我所拥有的:

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

当表'k'等于'table_x'时,我需要最内部的.append/for循环来添加另一个对象/值,除了名为'nestedTableInsert'的“字段”和“值”对象,它有自己的.append函数将在我的最终 JSON 中仅针对特定表创建另一个层,以便它看起来像这样,但具有正确的语法:

我正在努力做的事情:

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 如下所示(特别是 ea 唯一名称的“nestedTableInserts”):

[{
"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)

关于python - 使用 Python 创建复杂的 JSON,并根据条件创建一些嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55799314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com