Home > Back-end >  How can I parse JSON with many level of nesting?
How can I parse JSON with many level of nesting?

Time:07-07

I have a JSON file to be parsed to get specific information. Here is the JSON:

{
  "$schema" : "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.4.json",
  "version" : "2.1.0",
  "runs" : [ {
    "tool" : {
      "driver" : {
        "name" : "xxx ",
        "semanticVersion" : "ccc",
        "organization" : "www",
        "rules" : [ {
          "id" : "MISRA C  :2008 18-0-1",
          "name" : "18-0-1 The C library shall not be used."
        }, {
          "id" : "MISRA C  :2008 3-9-2",
          "name" : "3-9-2 Typedefs that indicate size and signedness should be used in place of the basic numerical types."
        }, {
          "id" : "MISRA C  :2008 0-1-2",
          "name" : "0-1-2 A project shall not contain infeasible paths."
        }, {
          "id" : "MISRA C  :2008 5-0-13",
          "name" : "5-0-13 The condition of an if-statement and the condition of an iteration-statement shall have type bool."
        }, {
          "id" : "MISRA C  :2008 5-2-4",
          "name" : "5-2-4 C-style casts (other than void casts) and functional notation casts (other than explicit constructor calls) shall not be used."
        }, {
          "id" : "MISRA C  :2008 7-1-1",
          "name" : "7-1-1 A variable which is not modified shall be const qualified."
        }, {
          "id" : "MISRA C  :2008 15-5-3",
          "name" : "15-5-3 The terminate() function shall not be called implicitly."
        }, {
          "id" : "MISRA C  :2008 15-3-2",
          "name" : "15-3-2 There should be at least one exception handler to catch all otherwise unhandled exceptions"
        } ]
      }
    },
    } 
}

I want a simple python list with all ids and its values within rules. Example expected output:

["MISRA C  :2008 18-0-1", "MISRA C  :2008 3-9-2", "MISRA C  :2008 0-1-2", "MISRA C  :2008 5-0-13", "MISRA C  :2008 5-2-4", "MISRA C  :2008 7-1-1", MISRA C  :2008 15-5-3", "MISRA C  :2008 15-3-2"]

How can I get this information?

What I have tried.. I am trying to navigate through the structure like below but it fails

with open(json_file, 'r') as fcc_file:
    fcc_data = json.load(fcc_file)
for channel in fcc_data:
    print(fcc_data[channel]["tool"])

Error:

TypeError: string indices must be integers

CodePudding user response:

You need to loop over the nested lists in runs and rules.

You can use a nested list comprehension to get the result you want.

result = [rule['id'] for run in fcc_data['runs'] for rule in run['tool']['driver']['rules']]

CodePudding user response:

You messed with input structure.

with open(json_file, 'r') as fcc_file:
    fcc_data = json.load(fcc_file)

for channel in fcc_data['runs']:  # iterating over a list now
    print([r['id'] for r in channel['tool']['driver']['rules']])
  • Related