如何将以下JSON重新格式化为特定值?

时间:2018-10-03 21:04:20

标签: python json python-3.x

我有以下JSON文件:

   a_lis= [
      {
        "id": "0394820934",
        "obj": [
          {
            "fruit": "mint",
            "origin": "brazil",
            "seller": "unknown",
            "record": false
          }
        ],
        "available": false,
        "count": 0
      },
      {
        "id": "0239840394",
        "obj": [
          {
            "fruit": "grapes",
            "origin": "origin",
            "seller": "unknown",
            "record": false
          },
          {
            "fruit": " ",
            "record": false
          },
          {
            "fruit": "ARN",
            "origin": "destination",
            "seller": "unknown",
            "record": true
          },
          {
            "fruit": " no ",
            "record": false
          },
          {
            "fruit": "apple",
            "origin": "unknown",
            "seller": "walmart",
            "record": false
          }
        ],
        "available": false,
        "count": 0
      },
      {
        "id": "0928293820982309",
        "obj": [
          {
            "fruit": "tomato",
            "origin": "COL",
            "seller": "unknown",
            "record": false
          },
          {
            "fruit": "Watermelon",
            "origin": "destination",
            "seller": "unknown",
            "record": false
          },
          {
            "fruit": " ",
            "record": false
          },
          {
            "fruit": "pear",
            "origin": "return_date",
            "seller": "walmart",
            "record": true
          }
        ],
        "available": false,
        "count": 0,
        "updated": 0
      },
      {
        "id": "23423423",
        "obj": [
          {
            "fruit": "10",
            "origin": "usa",
            "seller": "cotsco",
            "record": false
          }
        ],
        "available": false,
        "count": 0
      },
      {
        "id": "230948",
        "obj": [
          {
            "fruit": " ",
            "record": false
          },
          {
            "fruit": "pear",
            "origin": "adult_companion",
            "seller": "sears",
            "record": true
          },
          {
            "fruit": "apple",
            "record": false
          },
          {
            "fruit": "Watermelon",
            "origin": "Japan",
            "seller": "unknown",
            "record": true
          },
          {
            "fruit": " apple ",
            "record": false
          },
          {
            "fruit": " lemon ",
            "origin": "Canada",
            "seller": "unknown",
            "record": true
          },
          {
            "fruit": " the apple is good ",
            "record": false
          },
          {
            "fruit": " apple ",
            "origin": "COL",
            "seller": "sears",
            "record": true
          }
        ],
        "available": false,
        "count": 0
      }
    ]

上面的文件是字典的列表。如何复制所有包含“苹果”一词的子词典并将其附加到a_list中,并将该词替换为柠檬一词?例如:

a_lis = [
  {
    "id": "0394820934",
    "obj": [
      {
        "fruit": "mint",
        "origin": "brazil",
        "seller": "unknown",
        "record": false
      }
    ],
    "available": false,
    "count": 0
  },
  {
    "id": "0239840394",
    "obj": [
      {
        "fruit": "grapes",
        "origin": "origin",
        "seller": "unknown",
        "record": false
      },
      {
        "fruit": " ",
        "record": false
      },
      {
        "fruit": "ARN",
        "origin": "destination",
        "seller": "unknown",
        "record": true
      },
      {
        "fruit": " no ",
        "record": false
      },
      {
        "fruit": "apple",
        "origin": "unknown",
        "seller": "walmart",
        "record": false
      }
    ],
    "available": false,
    "count": 0
  },
  {
    "id": "0928293820982309",
    "obj": [
      {
        "fruit": "tomato",
        "origin": "COL",
        "seller": "unknown",
        "record": false
      },
      {
        "fruit": "Watermelon",
        "origin": "destination",
        "seller": "unknown",
        "record": false
      },
      {
        "fruit": " ",
        "record": false
      },
      {
        "fruit": "pear",
        "origin": "return_date",
        "seller": "walmart",
        "record": true
      }
    ],
    "available": false,
    "count": 0,
    "updated": 0
  },
  {
    "id": "23423423",
    "obj": [
      {
        "fruit": "10",
        "origin": "usa",
        "seller": "cotsco",
        "record": false
      }
    ],
    "available": false,
    "count": 0
  },
  {
    "id": "230948",
    "obj": [
      {
        "fruit": " ",
        "record": false
      },
      {
        "fruit": "pear",
        "origin": "adult_companion",
        "seller": "sears",
        "record": true
      },
      {
        "fruit": "apple",
        "record": false
      },
      {
        "fruit": "Watermelon",
        "origin": "Japan",
        "seller": "unknown",
        "record": true
      },
      {
        "fruit": " apple ",
        "record": false
      },
      {
        "fruit": " lemon ",
        "origin": "Canada",
        "seller": "unknown",
        "record": true
      },
      {
        "fruit": " the apple is good ",
        "record": false
      },
      {
        "fruit": " apple ",
        "origin": "COL",
        "seller": "sears",
        "record": true
      }
    ],
    "available": false,
    "count": 0
  },
  {
    "id": "0239840394",
    "obj": [
      {
        "fruit": "grapes",
        "origin": "origin",
        "seller": "unknown",
        "record": false
      },
      {
        "fruit": " ",
        "record": false
      },
      {
        "fruit": "ARN",
        "origin": "destination",
        "seller": "unknown",
        "record": true
      },
      {
        "fruit": " no ",
        "record": false
      },
      {
        "fruit": "lemon",
        "origin": "unknown",
        "seller": "walmart",
        "record": false
      }
    ],
    "available": false,
    "count": 0
  },
  {
    "id": "230948",
    "obj": [
      {
        "fruit": " ",
        "record": false
      },
      {
        "fruit": "pear",
        "origin": "adult_companion",
        "seller": "sears",
        "record": true
      },
      {
        "fruit": "lemon",
        "record": false
      },
      {
        "fruit": "Watermelon",
        "origin": "Japan",
        "seller": "unknown",
        "record": true
      },
      {
        "fruit": " lemon ",
        "record": false
      },
      {
        "fruit": " lemon ",
        "origin": "Canada",
        "seller": "unknown",
        "record": true
      },
      {
        "fruit": " the lemon is good ",
        "record": false
      },
      {
        "fruit": " lemon ",
        "origin": "COL",
        "seller": "sears",
        "record": true
      }
    ],
    "available": false,
    "count": 0
  }
]

换句话说,带有“苹果”一词的原始词典保留在列表中,而复制的版本附加在原始列表中,但带有柠檬一词?我试图按如下方式解析json文件:

import json
from pprint import pprint
json_output = open('file.json','r')
json_output = json.loads(json_output.read())

但是,我不知道如何复制字典并查看字典的嵌套结构。

1 个答案:

答案 0 :(得分:2)

鉴于此处的列表和词典的众多网络,对其进行迭代似乎非常费力。

解决问题的更简单方法是将其转换为字符串,将“ apple”替换为“ lemon”,然后将其转换为新的JSON文件,并将您的两个JSON文件合并为第三个文件,将两者合并

此代码适用于稍有更改的JSON文件:

import json

with open('a_lis-wquotes.json') as fa:
    a = json.load(fa)
    stra = (str(a))
    strb = stra.replace("apple", "lemon")
    strb = strb.replace("'", '"')
    with open('b_lis.json', 'w') as fb:
        fb.write(strb)

with open('a_lis-wquotes.json') as fa:
    a = json.load(fa)
    with open('b_lis.json') as fb:
        b = json.load(fb)
    c = a + b
    strc = str(c)
    strc = strc.replace("'", '"')
    with open('c_lis.json', 'w') as fc:
        fc.write(strc)

您会发现这里有很多引号。

这是出于以下几个原因: -Python的jsondecoder不想导入您的原始JSON,因为它在标记为“ true”和“ false”的记录周围缺少双引号。我在开始之前将这些引号添加到JSON文件中(因此文件名“ a_lis-wquotes.json”)。我在文本编辑器中使用查找/替换来完成此操作。如果由于文件太大而无法在文本编辑器中执行此操作,或者您有太多文件无法手动执行此操作,则可以在Python中执行相同的操作,方法是将该文件加载为文本文件,然后执行在那里替换文字。

  • 在将JSON加载到python中并以字符串形式进行操作时,JSON双引号已替换为单引号。这就是为什么我必须在将文件操作为JSON之前将strb写入文件。

  • 合并文件a和文件b并将JSON写入文件时,单引号/双引号问题再次出现。

总的来说,我认为输出与您期望的输出相匹配(不过,请再次忽略单引号/双引号的区别):

[   {   'available': 'false',
        'count': 0,
        'id': '0394820934',
        'obj': [   {   'fruit': 'mint',
                       'origin': 'brazil',
                       'record': 'false',
                       'seller': 'unknown'}]},
    {   'available': 'false',
        'count': 0,
        'id': '0239840394',
        'obj': [   {   'fruit': 'grapes',
                       'origin': 'origin',
                       'record': 'false',
                       'seller': 'unknown'},
                   {'fruit': ' ', 'record': 'false'},
                   {   'fruit': 'ARN',
                       'origin': 'destination',
                       'record': 'true',
                       'seller': 'unknown'},
                   {'fruit': ' no ', 'record': 'false'},
                   {   'fruit': 'apple',
                       'origin': 'unknown',
                       'record': 'false',
                       'seller': 'walmart'}]},
    {   'available': 'false',
        'count': 0,
        'id': '0928293820982309',
        'obj': [   {   'fruit': 'tomato',
                       'origin': 'COL',
                       'record': 'false',
                       'seller': 'unknown'},
                   {   'fruit': 'Watermelon',
                       'origin': 'destination',
                       'record': 'false',
                       'seller': 'unknown'},
                   {'fruit': ' ', 'record': 'false'},
                   {   'fruit': 'pear',
                       'origin': 'return_date',
                       'record': 'true',
                       'seller': 'walmart'}],
        'updated': 0},
    {   'available': 'false',
        'count': 0,
        'id': '23423423',
        'obj': [   {   'fruit': '10',
                       'origin': 'usa',
                       'record': 'false',
                       'seller': 'cotsco'}]},
    {   'available': 'false',
        'count': 0,
        'id': '230948',
        'obj': [   {'fruit': ' ', 'record': 'false'},
                   {   'fruit': 'pear',
                       'origin': 'adult_companion',
                       'record': 'true',
                       'seller': 'sears'},
                   {'fruit': 'apple', 'record': 'false'},
                   {   'fruit': 'Watermelon',
                       'origin': 'Japan',
                       'record': 'true',
                       'seller': 'unknown'},
                   {'fruit': ' apple ', 'record': 'false'},
                   {   'fruit': ' lemon ',
                       'origin': 'Canada',
                       'record': 'true',
                       'seller': 'unknown'},
                   {'fruit': ' the apple is good ', 'record': 'false'},
                   {   'fruit': ' apple ',
                       'origin': 'COL',
                       'record': 'true',
                       'seller': 'sears'}]},
    {   'available': 'false',
        'count': 0,
        'id': '0394820934',
        'obj': [   {   'fruit': 'mint',
                       'origin': 'brazil',
                       'record': 'false',
                       'seller': 'unknown'}]},
    {   'available': 'false',
        'count': 0,
        'id': '0239840394',
        'obj': [   {   'fruit': 'grapes',
                       'origin': 'origin',
                       'record': 'false',
                       'seller': 'unknown'},
                   {'fruit': ' ', 'record': 'false'},
                   {   'fruit': 'ARN',
                       'origin': 'destination',
                       'record': 'true',
                       'seller': 'unknown'},
                   {'fruit': ' no ', 'record': 'false'},
                   {   'fruit': 'lemon',
                       'origin': 'unknown',
                       'record': 'false',
                       'seller': 'walmart'}]},
    {   'available': 'false',
        'count': 0,
        'id': '0928293820982309',
        'obj': [   {   'fruit': 'tomato',
                       'origin': 'COL',
                       'record': 'false',
                       'seller': 'unknown'},
                   {   'fruit': 'Watermelon',
                       'origin': 'destination',
                       'record': 'false',
                       'seller': 'unknown'},
                   {'fruit': ' ', 'record': 'false'},
                   {   'fruit': 'pear',
                       'origin': 'return_date',
                       'record': 'true',
                       'seller': 'walmart'}],
        'updated': 0},
    {   'available': 'false',
        'count': 0,
        'id': '23423423',
        'obj': [   {   'fruit': '10',
                       'origin': 'usa',
                       'record': 'false',
                       'seller': 'cotsco'}]},
    {   'available': 'false',
        'count': 0,
        'id': '230948',
        'obj': [   {'fruit': ' ', 'record': 'false'},
                   {   'fruit': 'pear',
                       'origin': 'adult_companion',
                       'record': 'true',
                       'seller': 'sears'},
                   {'fruit': 'lemon', 'record': 'false'},
                   {   'fruit': 'Watermelon',
                       'origin': 'Japan',
                       'record': 'true',
                       'seller': 'unknown'},
                   {'fruit': ' lemon ', 'record': 'false'},
                   {   'fruit': ' lemon ',
                       'origin': 'Canada',
                       'record': 'true',
                       'seller': 'unknown'},
                   {'fruit': ' the lemon is good ', 'record': 'false'},
                   {   'fruit': ' lemon ',
                       'origin': 'COL',
                       'record': 'true',
                       'seller': 'sears'}]}]