如果其中一个键与条件匹配,如何删除嵌套的JSON对象?

问题描述 投票:0回答:2

我的json看起来像这样。我试图删除整个json对象,如果标签“类型”值匹配“日期”。如果是这样,它会将嵌套对象从“id”删除为“type”。

[
  {
    "text": "abcdef",
    "id": "369-03",
    "tags": [
      {
        "id": "P0",
        "start": "16",
        "end": "26",
        "text": "2080-11-30",
        "comment": "",
        "category": "DATE",
        "type": "Date"
      },
      {
        "id": "P1",
        "start": "48",
        "end": "52",
        "text": "Owen",
        "comment": "",
        "category": "NAME",
        "type": "PATIENT"
      },
      {
        "id": "P5",
        "start": "1664",
        "end": "1683",
        "text": "William Seth Potter",
        "comment": "",
        "category": "NAME",
        "type": "Name"
      },
      {
        "id": "P2",
        "start": "58",
        "end": "60",
        "text": "63",
        "comment": "",
        "category": "AGE",
        "type": "AGE"
      }
    ]
  },
  {
    "text": "12345",
    "id": "354-02",
    "tags": [
      {
        "id": "P0",
        "start": "16",
        "end": "26",
        "text": "2095-09-04",
        "comment": "",
        "category": "DATE",
        "type": "Date"
      },
      {
        "id": "P11",
        "start": "3020",
        "end": "3023",
        "text": "CCH",
        "comment": "",
        "category": "LOCATION",
        "type": "HOSPITAL"
      },
      {
        "id": "P12",
        "start": "3238",
        "end": "3240",
        "text": "GH",
        "comment": "",
        "category": "LOCATION",
        "type": "HOSPITAL"
      }
    ]
  }
]

例如,我希望在匹配后删除此对象:

 {
            "id": "P0",
            "start": "16",
            "end": "26",
            "text": "2080-11-30",
            "comment": "",
            "category": "DATE",
            "type": "Date"
          }

然后它检查同一父对象“tags”中的下一个对象。

到目前为止,我已尝试使用for循环进行pop和delete,但我无法进入嵌套对象并删除所有符合子标记条件的对象。

这是:

    data = json.load(f)

   for values in data:
    for tags in values['tags']:
           tags.pop('type','Date')

 for i in range(len(data)):
     if data[i]["type"] == "Date":
         data.pop(i)

但是在最后一个例子当然我得到了一个keyerror因为我没有迭代它直到标签类型。

python json nested
2个回答
0
投票

通过过滤列表理解将允许您执行此操作:

for values in data:
    values['tags'] = [tag for tag in values['tags'] if tag['type'] == "Date"]

0
投票
package com.inlet.ifserver;

import com.filethis.common.util.JsonEncodeDecode;

import java.util.Map;
import java.util.List;

public class x {

    static String json = "[\n"+
            "  {\n"+
            "    \"text\": \"abcdef\",\n"+
            "    \"id\": \"369-03\",\n"+
            "    \"tags\": [\n"+
            "      {\n"+
            "        \"id\": \"P0\",\n"+
            "        \"start\": \"16\",\n"+
            "        \"end\": \"26\",\n"+
            "        \"text\": \"2080-11-30\",\n"+
            "        \"comment\": \"\",\n"+
            "        \"category\": \"DATE\",\n"+
            "        \"type\": \"Date\"\n"+
            "      },\n"+
            "      {\n"+
            "        \"id\": \"P1\",\n"+
            "        \"start\": \"48\",\n"+
            "        \"end\": \"52\",\n"+
            "        \"text\": \"Owen\",\n"+
            "        \"comment\": \"\",\n"+
            "        \"category\": \"NAME\",\n"+
            "        \"type\": \"PATIENT\"\n"+
            "      },\n"+
            "      {\n"+
            "        \"id\": \"P5\",\n"+
            "        \"start\": \"1664\",\n"+
            "        \"end\": \"1683\",\n"+
            "        \"text\": \"William Seth Potter\",\n"+
            "        \"comment\": \"\",\n"+
            "        \"category\": \"NAME\",\n"+
            "        \"type\": \"Name\"\n"+
            "      },\n"+
            "      {\n"+
            "        \"id\": \"P2\",\n"+
            "        \"start\": \"58\",\n"+
            "        \"end\": \"60\",\n"+
            "        \"text\": \"63\",\n"+
            "        \"comment\": \"\",\n"+
            "        \"category\": \"AGE\",\n"+
            "        \"type\": \"AGE\"\n"+
            "      }\n"+
            "    ]\n"+
            "  },\n"+
            "  {\n"+
            "    \"text\": \"12345\",\n"+
            "    \"id\": \"354-02\",\n"+
            "    \"tags\": [\n"+
            "      {\n"+
            "        \"id\": \"P0\",\n"+
            "        \"start\": \"16\",\n"+
            "        \"end\": \"26\",\n"+
            "        \"text\": \"2095-09-04\",\n"+
            "        \"comment\": \"\",\n"+
            "        \"category\": \"DATE\",\n"+
            "        \"type\": \"Date\"\n"+
            "      },\n"+
            "      {\n"+
            "        \"id\": \"P11\",\n"+
            "        \"start\": \"3020\",\n"+
            "        \"end\": \"3023\",\n"+
            "        \"text\": \"CCH\",\n"+
            "        \"comment\": \"\",\n"+
            "        \"category\": \"LOCATION\",\n"+
            "        \"type\": \"HOSPITAL\"\n"+
            "      },\n"+
            "      {\n"+
            "        \"id\": \"P12\",\n"+
            "        \"start\": \"3238\",\n"+
            "        \"end\": \"3240\",\n"+
            "        \"text\": \"GH\",\n"+
            "        \"comment\": \"\",\n"+
            "        \"category\": \"LOCATION\",\n"+
            "        \"type\": \"HOSPITAL\"\n"+
            "      }\n"+
            "    ]\n"+
            "  }\n"+
            "]";

    public static void main(String[] args) {

        List<Map<String, Object>> data = (List<Map<String, Object>>) JsonEncodeDecode.decode(json);
        for (Map<String, Object> entry : data) {
            List<Map<String, String>> tags = (List<Map<String, String>>)entry.get("tags");
            for (int i = tags.size() ; i > 0 ; i--) {
                Map<String, String> tag = tags.get(i-1);
                if (tag.get("type").equals("Date")) {
                    System.out.println("Deleting record with id: " + tag.get("id"));
                    tags.remove(i-1);
                }
            }
        }
        System.out.println(JsonEncodeDecode.encode(data, true));
    }
}

输出:

Deleting record with id: P0
Deleting record with id: P0
[ {
  "text" : "abcdef",
  "id" : "369-03",
  "tags" : [ {
    "id" : "P1",
    "start" : "48",
    "end" : "52",
    "text" : "Owen",
    "comment" : "",
    "category" : "NAME",
    "type" : "PATIENT"
  }, {
    "id" : "P5",
    "start" : "1664",
    "end" : "1683",
    "text" : "William Seth Potter",
    "comment" : "",
    "category" : "NAME",
    "type" : "Name"
  }, {
    "id" : "P2",
    "start" : "58",
    "end" : "60",
    "text" : "63",
    "comment" : "",
    "category" : "AGE",
    "type" : "AGE"
  } ]
}, {
  "text" : "12345",
  "id" : "354-02",
  "tags" : [ {
    "id" : "P11",
    "start" : "3020",
    "end" : "3023",
    "text" : "CCH",
    "comment" : "",
    "category" : "LOCATION",
    "type" : "HOSPITAL"
  }, {
    "id" : "P12",
    "start" : "3238",
    "end" : "3240",
    "text" : "GH",
    "comment" : "",
    "category" : "LOCATION",
    "type" : "HOSPITAL"
  } ]
} ]
© www.soinside.com 2019 - 2024. All rights reserved.