JSON查询表达式(JMESPath),用于从字典的嵌套列表中删除键/值

时间:2019-11-28 17:08:53

标签: json jmespath

我在JSON中具有以下数据结构,这是一个字典,其中每个元素都是字典列表。

{
    "383e36d1-32e5-4705-8271-fa5e9e2ad538": [
        {
        "label": "blah",
        "group_label": "group_a",
        "id": "id_blah"
        },
        {
        "label": "bloh",
        "group_label": "group_b",
        "id": "id_bloh"
        }
    ],
    "38b8293c-00c4-4bcf-91eb-440da656c653": [
        {
        "label": "bim",
        "group_label": "group_c",
        "id": "id_bim"
        },
        {
        "label": "bam",
        "group_label": "group_d",
        "id": "id_bam"
        },
   ...
}

并且我需要一个JMESPath查询表达式才能将其转换为:

{
    "383e36d1-32e5-4705-8271-fa5e9e2ad538": [
        {
        "label": "blah",
        "group_label": "group_a",
        },
        {
        "label": "bloh",
        "group_label": "group_b",
        }
    ],
    "38b8293c-00c4-4bcf-91eb-440da656c653": [
        {
        "label": "bim",
        "group_label": "group_c",
        },
        {
        "label": "bam",
        "group_label": "group_d",
        },
   ...
}

基本上保持相同的结构,但是从每个条目中删除了id

1 个答案:

答案 0 :(得分:0)

在Node.js中,您可以使用以下代码:

var json = {
    "383e36d1-32e5-4705-8271-fa5e9e2ad538": [
        {
        "label": "blah",
        "group_label": "group_a",
        "id": "id_blah"
        },
        {
        "label": "bloh",
        "group_label": "group_b",
        "id": "id_bloh"
        }
    ],
    "38b8293c-00c4-4bcf-91eb-440da656c653": [
        {
        "label": "bim",
        "group_label": "group_c",
        "id": "id_bim"
        },
        {
        "label": "bam",
        "group_label": "group_d",
        "id": "id_bam"
        }
    ]
};

for (var key in json) {
    for (var index = 0; index < json[key].length; index++) {
        delete json[key][index].id;
    }
}
console.log(json);
相关问题