Python 删除JSON数据(如果在主JSON数据中可用)

Python 删除JSON数据(如果在主JSON数据中可用),python,json,delete-operator,Python,Json,Delete Operator,我有两个json数据,其中json_main和json1如下所示。我想检查json1,如果json_主数据中有任何可用的详细信息,那么应该删除确切的详细信息和预期的结果,我看起来像下面显示的“output-Result_-json” 例如:在json1数据“字段”中:{ “完整地址”:“数据2”, “hz”:“text2”, “ot”:“doc2”在json_主数据中可用。因此我想从json1数据中删除它 我尝试使用python代码,但我不知道如何与json_主数据进行比较,并在json1数据中

我有两个json数据,其中json_main和json1如下所示。我想检查json1,如果json_主数据中有任何可用的详细信息,那么应该删除确切的详细信息和预期的结果,我看起来像下面显示的“output-Result_-json”

例如:在json1数据“字段”中:{ “完整地址”:“数据2”, “hz”:“text2”, “ot”:“doc2”在json_主数据中可用。因此我想从json1数据中删除它

我尝试使用python代码,但我不知道如何与json_主数据进行比较,并在json1数据中删除

json\u main

 [
  {
    "fields": {
      "Full Address": "data1",
      "hz": "text1",
      "ot": "doc1"
   }
  },
  {
  "fields": {
    "Full Address": "data2",
    "hz": "text2",
    "ot": "doc2"
     }
   }
   ]
 [
  {
  "fields": {
    "Full Address": "data3",
    "hz": "text3",
    "ot": "doc3"
  }
 }
 ]
  with open('writing_file.json', 'w') as w:
  with open('reading_file.json', 'r') as r:
    for line in r:
        element = json.loads(line.strip())
        if 'data2' in element:
            del element['data2']
        w.write(json.dumps(element))
json1

 [
  {
    "fields": {
      "Full Address": "data2",
      "hz": "text2",
      "ot": "doc2"
   }
  },
  {
  "fields": {
    "Full Address": "data3",
    "hz": "text3",
    "ot": "doc3"
  }
 }
 ]
结果结果\u json

 [
  {
    "fields": {
      "Full Address": "data1",
      "hz": "text1",
      "ot": "doc1"
   }
  },
  {
  "fields": {
    "Full Address": "data2",
    "hz": "text2",
    "ot": "doc2"
     }
   }
   ]
 [
  {
  "fields": {
    "Full Address": "data3",
    "hz": "text3",
    "ot": "doc3"
  }
 }
 ]
  with open('writing_file.json', 'w') as w:
  with open('reading_file.json', 'r') as r:
    for line in r:
        element = json.loads(line.strip())
        if 'data2' in element:
            del element['data2']
        w.write(json.dumps(element))
Python

 [
  {
    "fields": {
      "Full Address": "data1",
      "hz": "text1",
      "ot": "doc1"
   }
  },
  {
  "fields": {
    "Full Address": "data2",
    "hz": "text2",
    "ot": "doc2"
     }
   }
   ]
 [
  {
  "fields": {
    "Full Address": "data3",
    "hz": "text3",
    "ot": "doc3"
  }
 }
 ]
  with open('writing_file.json', 'w') as w:
  with open('reading_file.json', 'r') as r:
    for line in r:
        element = json.loads(line.strip())
        if 'data2' in element:
            del element['data2']
        w.write(json.dumps(element))

我想回答我自己的问题,首先我将从“json_main”生成“Full Address”的值作为唯一键,即“data1”、“data2”

唯一_键=[“数据1”,“数据2”]

过滤“json1”的数据。请参阅下面的python代码

python代码

  import json
  import sys
  
  unique_key = [elt["fields"]["Full Address"] for elt in json_main]
  print( unique key)
  Output =  ["data1", "data2"]


  json1 = [
        {
      "fields": {
           "Full Address": "data2",
           "hz": "text2",
           "ot": "doc2"
          }
          },
         {
     "fields": {
           "Full Address": "data3",
           "hz": "text3",
           "ot": "doc3"
               }
               }
             ]


 #  delete json objects.
 data2 = [x for x in json1 if x['fields']['Full Address'] not in unique_key]

 print(data2)

  data2 = [
          {
       "fields": {
           "Full Address": "data3",
           "hz": "text3",
           "ot": "doc3"
                 }
                }
             ]

如果只有一个字段是相同的呢?为什么要显式地检查
'data2'
?那么结果在json正文中没有给出任何内容。