0

I am trying to return the fields and values of a specific key but getting error Here is the sample json format:

"results": [
    {
      "time": "00:00",
      "app_name": "dcg",
      "avg": "7717"
    },
    {
      "time": "00:00",
      "app_name": "pds",
      "avg": "75.40223463687151"
    },
    {
      "time": "00:00",
      "app_name": "rdts",
      "avg": "1481.5555555555557"
    },
    {
      "time": "00:00",
      "app_name": "slbl",
      "avg": "786"
    },
    {
      "time": "01:00",
      "app_name": "pds",
      "avg": "36.4765625"
    }

here is my code

import json

json_data=open("some.json")
jdata = json.load(json_data)

for k, v in jdata.results.items():
    for k1, v1 in v.items():
        print(k1)
            print(v1)

Please note results is key and time field having multiple entries with same value.

lucky
  • 9
  • 1
  • 10
  • 2
    What's your error? My money is on `IndentationError`. – Jordan Singer Feb 01 '19 at 16:41
  • @JordanSinger getting error as python pj.py Traceback (most recent call last): File "pj.py", line 7, in for (k, v) in jdata.results.items(): AttributeError: 'dict' object has no attribute 'results' – lucky Feb 01 '19 at 16:44
  • it might not accepting results as per dict object. i am using jdata.results.items coz having multiple keys in json file and i wants to extract fields and values from results key only – lucky Feb 01 '19 at 16:46
  • @lucky: Try if my answer helps. If not, drop in a comment and I will try to modify – Sheldore Feb 01 '19 at 16:47
  • @Bazingaa i tried and it works. it returns list of the keys and values. what i need to do if i need in key value pair or in dict format? [ "time" :"00", "app_name":"value" , "avg":"value"] – lucky Feb 01 '19 at 16:53
  • @lucky: You can't have `[ "time" :"00", "app_name":"value" , "avg":"value"] ` format in a list. YOu have to use each key-value pair as a sub-dictionary – Sheldore Feb 01 '19 at 17:08
  • @lucky: IS your problem addressed? Or do I need to change my answer? – Sheldore Feb 01 '19 at 17:12

2 Answers2

0

Apart from fixing the broken indent, you can try the following. results is a key of your dictionary whose value can be accessed as jdata["results"].

for v in jdata["results"]:
    for k1, v1 in v.items():
        print(k1)
        print(v1)

EDIT: To store them in a list, you can do

result = []

for v in jdata["results"]:
    for k1, v1 in v.items():
        result.append({"k1":v1})

[{'k1': '00:00'},
 {'k1': 'dcg'},
 {'k1': '7717'},
 {'k1': '00:00'},
 {'k1': 'pds'},
 {'k1': '75.40223463687151'},
 {'k1': '00:00'},
 {'k1': 'rdts'},
 {'k1': '1481.5555555555557'},
 {'k1': '00:00'},
 {'k1': 'slbl'},
 {'k1': '786'},
 {'k1': '01:00'},
 {'k1': 'pds'},
 {'k1': '36.4765625'}]        
Sheldore
  • 37,862
  • 7
  • 57
  • 71
0

I had to put your JSON in a containing set of brackets {} otherwise the JSON processor can't parse it....

i.e...

{ 'result' : [...] }
Travis
  • 552
  • 1
  • 8
  • 14
  • yes. sorry for not putting correct sample but the results key is one of the part of json file. there are multiple keys and results key is one of them. hence i only posted results section – lucky Feb 01 '19 at 17:01