Geoff_S
Geoff_S

Reputation: 5107

How to filter json and print into new json file?

I have the below, partially incomplete python code, which I'm trying to use to simply parse this JSON object and write the results into a new json file, or output the results to a console even.

I just want to return only the nodes that contain price_cat of 'C', and I'd also like to just remove the entire 'History' node from each object completely if possible.

What am I doing wrong and how can I simply achieve this?

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)

Upvotes: 0

Views: 299

Answers (2)

pythomatic
pythomatic

Reputation: 657

You're not looking in the prices list in your dictionary:

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)

This results in the answer you're looking for:

[
    {" 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"}]}
]

Upvotes: 3

Rahmi Pruitt
Rahmi Pruitt

Reputation: 601

it seems you forgot to index one level down before trying to find the price category. It can be helpful to write it this way.

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)

happens to the best of us :).

Upvotes: 2

Related Questions