Home > Back-end >  how to read nested lists information from a Json file using Python
how to read nested lists information from a Json file using Python

Time:05-05

Here is a part of my Jason file, and I want to read "information" under "runs" -> "results" -> "properties"

I am trying the following:

with open(inputFile, "r") as readFile:
    data = json.load(readFile)
    print(type(data))
    print("Run data type is: ",type(data['runs']))
    #print("properties data type is: ", type(data['runs']['properties']))
    # error:     print("results data type is: ", type(data['runs']['properties']))TypeError: list indices must be integers or slices, not str
    for info in data['runs']:
        res = info.get('results',{})
        #res = info.get('results', {}).get('properties', None)
        #Error: AttributeError: 'list' object has no attribute 'get'
        #inf = info.get('properties')
        print(res)

All the parts that I have commented is not working. and I added also the error message how can i read "information" in a loop?

{
  "$schema" : "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.4.json",
  "version" : "2.1.0",
  "runs" : [ {
    "tool" : { ...},
    "artifacts" : [ ...],
    "results" : [ {
      "ruleId" : "DECL_MISMATCH",
      "ruleIndex" : 0,
      "message" : {
        "text" : "XXXXX"
      },
      "level" : "error",
      "baselineState" : "unchanged",
      "rank" : 100,
      "kind" : "fail",
      "properties" : {
        "tags" : [ "databaseId", "metaFamily", "family", "group", "information", "severity", "status", "comment", "justified", "assignedTo", "ticketKey", "color" ],
        "databaseId" : 54496,
        "metaFamily" : "Defect",
        "family" : "Defect",
        "group" : "Programming",
        "information" : "Impact: High",
        "severity" : "Unset",
        "status" : "Unreviewed",
        "comment" : "",
        "justified" : false,
        "color" : "RED"
      },
      "locations" : [ {
        "physicalLocation" : {
          "artifactLocation" : {
            "index" : 0
          }
        },
        "logicalLocations" : [ {
          "fullyQualifiedName" : "File Scope",
          "kind" : "function"
        } ]
      } ]
    } ]
  } ]
}

CodePudding user response:

While you're trying to access the key properties which is inside a list, you have to set the index number. In this json you've posted the index number can be 0. So the code probably should be like this:

with open(inputFile, "r") as readFile:
    data = json.load(readFile)
    print(type(data))
    print("Run data type is: ",type(data['runs']))
    #print("properties data type is: ", type(data['runs']['properties']))
    # error:     print("results data type is: ", type(data['runs']['properties']))TypeError: list indices must be integers or slices, not str
    for info in data['runs']:
        # res = info.get('results',{})
        res = info.get('results', {})[0].get('properties', None)
        #inf = info.get('properties')
        print(res)

CodePudding user response:

for run in data['runs']:
    for result in run['results']:
        properties = result['properties']
        print("information = {}".format(properties['information']))
  • Related