-1

Can anybody help me to get data from the json below.I have the json data in the format below

[
  {
    "methods": [
      {
        "parametersTypes": [
          "int",
          "Menu"
        ],
        "sourceFile": {
          "file": {
            "path": "/mnt/c/anyplace-master/android/actionbarsherlock/src/android/support/v4/app/Watson.java"
      }
    },
    "metricsValues": {
      "MethodLinesOfCode": 33.0,
      "CyclomaticComplexity": 13.0,
      "CouplingDispersion": 0.6666666666666666,
      "MaxNesting": 5.0,
      "ChangingClasses": 0.0,
      "ParameterCount": 2.0,
      "CouplingIntensity": 3.0,
      "NumberOfAccessedVariables": 13.0,
      "MaxCallChain": 1.0,
      "ChangingMethods": 0.0
    },
    "fullyQualifiedName": "android.support.v4.app.Watson.onCreatePanelMenu",
    "smells": [
      {
        "name": "LongMethod",
        "reason": "MLOC > 6.74646840148693",
        "startingLine": 39,
        "endingLine": 82
      }
    ]

I need just the value of the key "name" which is "longMethod"

"name": "LongMethod"

NB: this block is a part of my JSON file and it repeats

Thank you in advance!

Rheatey Bash
  • 779
  • 6
  • 17
Salma
  • 43
  • 7
  • 1
    What problems did you have using the `json` module from the standard library? https://docs.python.org/3/library/json.html – cdarke May 12 '19 at 20:58
  • can you please fix this module /part of json which is repeating, in given example it's incomplete – sahasrara62 May 12 '19 at 21:01
  • 1
    Possible duplicate of [Parse JSON in Python](https://stackoverflow.com/questions/7771011/parse-json-in-python) – Andrew Allen May 12 '19 at 21:02

3 Answers3

0

You can open your file with json :

import json
with open('your_file.json') as f:
    data = json.load(f)

and access your variable with :

data[0]['methods']['smells'][0]['name']
F Blanchet
  • 1,430
  • 3
  • 21
  • 32
0

Assuming my educated guess about the incomplete json is correct this is what you do

s="""[
    {
        "methods": 
        [
            {
                "parametersTypes":
                [
                    "int",
                    "Menu"
                ],
                "sourceFile": 
                {
                    "file": 
                    {
                        "path": "/mnt/c/anyplace-master/android/actionbarsherlock/src/android/support/v4/app/Watson.java"
                    }
                },
                "metricsValues": 
                {
                  "MethodLinesOfCode": 33.0,
                  "CyclomaticComplexity": 13.0,
                  "CouplingDispersion": 0.6666666666666666,
                  "MaxNesting": 5.0,
                  "ChangingClasses": 0.0,
                  "ParameterCount": 2.0,
                  "CouplingIntensity": 3.0,
                  "NumberOfAccessedVariables": 13.0,
                  "MaxCallChain": 1.0,
                  "ChangingMethods": 0.0
                },
                "fullyQualifiedName": "android.support.v4.app.Watson.onCreatePanelMenu",
                "smells": 
                [
                    {
                        "name": "LongMethod",
                        "reason": "MLOC > 6.74646840148693",
                        "startingLine": 39,
                        "endingLine": 82
                    }
                ]
            }
        ]
    }
]"""

import json
d=json.loads(s)
print(d[0]["methods"][0]["smells"][0]["name"])
Lord Wolfenstein
  • 261
  • 2
  • 14
  • thank but it return a list like this LongMethod LongMethod LongMethod LongMethod LongMethod LongMethod LongMethod LongMethod LongMethod LongMethod i need the name of smells on each block – Salma May 12 '19 at 21:13
0

This should work

data = [
  {
    "methods": [
      {
        "parametersTypes": [
          "int",
          "Menu"
        ],
        "sourceFile": {
          "file": {
            "path": "/mnt/c/anyplace-master/android/actionbarsherlock/src/android/support/v4/app/Watson.java"
      }
    },
    "metricsValues": {
      "MethodLinesOfCode": 33.0,
      "CyclomaticComplexity": 13.0,
      "CouplingDispersion": 0.6666666666666666,
      "MaxNesting": 5.0,
      "ChangingClasses": 0.0,
      "ParameterCount": 2.0,
      "CouplingIntensity": 3.0,
      "NumberOfAccessedVariables": 13.0,
      "MaxCallChain": 1.0,
      "ChangingMethods": 0.0
    },
    "fullyQualifiedName": "android.support.v4.app.Watson.onCreatePanelMenu",
    "smells": [
      {
        "name": "LongMethod",
        "reason": "MLOC > 6.74646840148693",
        "startingLine": 39,
        "endingLine": 82
      }
    ]
          }
          ]
  }
]

print(data[0]['methods'][0]['smells'][0]['name'])

You might need to make it dynamic by using indexing methods. You can use type method of python to check the subsquent type of your data such as type(data), type(data[0]), type(data[0]['methods']), it will help you understand it better.

Rheatey Bash
  • 779
  • 6
  • 17