PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 25, 2022

[FIXED] How can I parse JSON with many level of nesting?

 July 25, 2022     json, python     No comments   

Issue

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

Solution

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']]


Answered By - Barmar
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing