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

我有下面部分不完整的 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)


慕哥6287543
浏览 126回答 2
2回答

郎朗坤

您没有在字典中查找价格表:import jsoninput_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 objectsinput_dict = json.loads(input_json)#Filter python objects with list comprehensionsoutput_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 jsonoutput_json = json.dumps(output_dict)#Show jsonprint (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"}]}]

慕斯709654

在尝试查找价格类别之前,您似乎忘记将索引向下一级索引。这样写会很有帮助。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)发生在我们最好的人身上:)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python