Home > Mobile >  Querying a nested JSON file in Python without Indexing
Querying a nested JSON file in Python without Indexing

Time:04-08

I have the below Json file which I need to query to get the values of the keys inside 'validations' in a list

for example the column_values_not_null output will need to be this:

['lu_name', 'transaction_amount']

  "validation_file_name": "ctm",
  "connection_type": "s3",
  "low_threshold": 500000,
  "high_threshold": 1000000,
  "frequency": "weekly",
  "validations": [
    {
       "columns_to_match_ordered_list" :[
        "lu_name",
        "site_name",
        "transaction_date_time",
        "margin",
        "transaction_currency_code",
        "reversal_indicator_description",
        "reversal_amount",
        "original_amount"
      ]
    },
    {
       "column_values_not_null":[
        "lu_name",
        "transaction_amount"
      ]
  },
    {
      "column_values_not_duplicate": [
         "lu_name",
        "response_code_description"
      ]
    }
  ]

I am able to do the below but I need to do this without using the index value

f = open('test.json')

json_content = json.load(f)

print(json_content['validations'][1]['column_values_not_null']) 

CodePudding user response:

Get a list by quering the validations key. The sum( ,[]) are used to flat the list (as required by the condition "without using the index value" if got it right), for details about it with pros and cons see doc.

data = # 

def validations(data: dict, key_query: str) -> list:
    for k, v in data.items():
        if k == 'validations':
            return sum(sum([list(d.values()) for d in v if key_query in d], []), [])


print(validations(data, query='column_values_not_null'))

# ['lu_name', 'transaction_amount']
  • Related