为了账号安全,请及时绑定邮箱和手机立即绑定

如何过滤 json 并打印到新的 json 文件中?

如何过滤 json 并打印到新的 json 文件中?

慕哥6287543 2023-08-08 10:52:15
我有下面部分不完整的 python 代码,我试图用它来简单地解析这个 JSON 对象并将结果写入一个新的 json 文件,或者甚至将结果输出到控制台。我只想返回包含price_catof的节点,并且如果可能的话,'C'我还想从每个对象中完全删除整个节点。'History'我做错了什么以及我怎样才能简单地实现这一目标?import json input_json = "" "   [       {           " type ": " 1 ",           " name ": " name 1 ",           "history":[             {               "expiration_date":"9999-12-31",               "effective_date":"2017-01-01"             }            ],            "prices":[             {               "price":"3.00",               "price_cat":"C",             }           ]       },       {           " type ": " 2 ",           " name ": " name 2 ",           "history":[             {               "expiration_date":"9999-12-31",               "effective_date":"2017-01-01"             }            ],            "prices":[             {               "price":"3.00",               "price_cat":"A",             },             {               "price":"3.00",               "price_cat":"C",             }           ]       },       {           " type ": " 1 ",           " name ": " name 3 ",           "history":[             {               "expiration_date":"9999-12-31",               "effective_date":"2017-01-01"             }            ],            "prices":[             {               "price":"3.00",               "price_cat":"B",             }           ]       }   ]" ""   #Transform json input to python objects     input_dict = json.loads (input_json)   #Filter python objects with list comprehensions     output_dict =[x for x in input_dict if x['price_cat'] == 'C']   #Transform python object back into json     output_json = json.dumps (output_dict)   #Show json       print (output_json)
查看完整描述

2 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

您没有在字典中查找价格表:


import json

input_json = """

[

    {

        " type ":" 1 ",

        " name ":" name 1 ",

        "history":[

             {

                "expiration_date":"9999-12-31",

                "effective_date":"2017-01-01"

             }

        ],

        "prices":[

             {

                "price":"3.00",

                "price_cat":"C"

             }]

        },

        {

        " type ":" 2 ",

        " name ":" name 2 ",

        "history":[

             {

                "expiration_date":"9999-12-31",

                "effective_date":"2017-01-01"

             }],

        "prices":[

             {

                "price":"3.00",

                "price_cat":"A"

             },

             {

                "price":"3.00",

                "price_cat":"C"

             }

        ]

        },

            {

            " type ":" 1 ",

            " name ":" name 3 ",

            "history":[

                 {

                    "expiration_date":"9999-12-31",

                    "effective_date":"2017-01-01"

                 }

            ],

            "prices":[

                 {

                    "price":"3.00",

                    "price_cat":"B"

                 }

            ]

    }

]"""


#Transform json input to python objects

input_dict = json.loads(input_json)

#Filter python objects with list comprehensions

output_dict = []

for input_subdict in input_dict:

    matching_prices = []

    for price in input_subdict['prices']:

        if price['price_cat'] == 'C':

            matching_prices.append(price)

    if len(matching_prices) > 0:

        input_subdict['prices'] = matching_prices

        output_dict.append(input_subdict)


#Transform python object back into json


output_json = json.dumps(output_dict)

#Show json

print (output_json)

这会产生您正在寻找的答案:


[

    {" type ": " 1 ", " name ": " name 1 ", "history": [{"expiration_date": "9999-12-31", "effective_date": "2017-01-01"}], "prices": [{"price": "3.00", "price_cat": "C"}]}, 

    {" type ": " 2 ", " name ": " name 2 ", "history": [{"expiration_date": "9999-12-31", "effective_date": "2017-01-01"}], "prices": [{"price": "3.00", "price_cat": "C"}]}

]


查看完整回答
反对 回复 2023-08-08
?
慕斯709654

TA贡献1840条经验 获得超5个赞

在尝试查找价格类别之前,您似乎忘记将索引向下一级索引。这样写会很有帮助。


parseObjects = []

for jObject in input_json:

  for price in jObject["prices"]:

    if price["price_cat"] == "C":

      if "history" in jObject:

        del jObject["history"]

      parseObjects.append(jObject)

发生在我们最好的人身上:)。


查看完整回答
反对 回复 2023-08-08
  • 2 回答
  • 0 关注
  • 125 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信