Home > other >  Getting specific value from list inside dictionary (JSON)
Getting specific value from list inside dictionary (JSON)

Time:12-11

I have just started with JSON and am trying to create a program that gets data from a weather station and logs it in my python program. However, the data is structured in this way:

 "timeSeries":[
       {
          "validTime":"2021-12-07T14:00:00Z",
          "parameters":[
             {
                "name":"spp",
                "levelType":"hl",
                "level":0,
                "unit":"percent",
                "values":[
                   -9.0
                ]
             },
             {
                "name":"pcat",
                "levelType":"hl",
                "level":0,
                "unit":"category",
                "values":[
                   0
                ]
             },
             {
                "name":"pmin",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"pmean",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"pmax",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"pmedian",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"tcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   7
                ]
             },
             {
                "name":"lcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   6
                ]
             },
             {
                "name":"mcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   3.0
                ]
             },
             {
                "name":"hcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   1
                ]
             },
             {
                "name":"t",
                "levelType":"hl",
                "level":2,
                "unit":"Cel",
                "values":[
                   -11.3
                ]
             },
             {
                "name":"msl",
                "levelType":"hmsl",
                "level":0,
                "unit":"hPa",
                "values":[
                   1017.5
                ]
             },
             {
                "name":"vis",
                "levelType":"hl",
                "level":2,
                "unit":"km",
                "values":[
                   50.0
                ]
             },
             {
                "name":"wd",
                "levelType":"hl",
                "level":10,
                "unit":"degree",
                "values":[
                   84
                ]
             },
             {
                "name":"ws",
                "levelType":"hl",
                "level":10,
                "unit":"m/s",
                "values":[
                   1.6
                ]
             },
             {
                "name":"r",
                "levelType":"hl",
                "level":2,
                "unit":"percent",
                "values":[
                   79
                ]
             },
             {
                "name":"tstm",
                "levelType":"hl",
                "level":0,
                "unit":"percent",
                "values":[
                   0.0
                ]
             },
             {
                "name":"gust",
                "levelType":"hl",
                "level":10,
                "unit":"m/s",
                "values":[
                   3.8
                ]
             },
             {
                "name":"Wsymb2",
                "levelType":"hl",
                "level":0,
                "unit":"category",
                "values":[
                   4
                ]
             }
          ]
       },
       {
          "validTime":"2021-12-07T15:00:00Z",
          "parameters":[
             {
                "name":"spp",
                "levelType":"hl",
                "level":0,
                "unit":"percent",
                "values":[
                   -9
                ]
             },
             {
                "name":"pcat",
                "levelType":"hl",
                "level":0,
                "unit":"category",
                "values":[
                   0
                ]
             },
             {
                "name":"pmin",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"pmean",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"pmax",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"pmedian",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"tcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   8
                ]
             },
             {
                "name":"lcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   8
                ]
             },
             {
                "name":"mcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   3
                ]
             },
             {
                "name":"hcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   1
                ]
             },
             {
                "name":"t",
                "levelType":"hl",
                "level":2,
                "unit":"Cel",
                "values":[
                   -11.2
                ]
             },
             {
                "name":"msl",
                "levelType":"hmsl",
                "level":0,
                "unit":"hPa",
                "values":[
                   1017.5
                ]
             },
             {
                "name":"vis",
                "levelType":"hl",
                "level":2,
                "unit":"km",
                "values":[
                   50.0
                ]
             },
             {
                "name":"wd",
                "levelType":"hl",
                "level":10,
                "unit":"degree",
                "values":[
                   85
                ]
             },
             {
                "name":"ws",
                "levelType":"hl",
                "level":10,
                "unit":"m/s",
                "values":[
                   1.7
                ]
             },
             {
                "name":"r",
                "levelType":"hl",
                "level":2,
                "unit":"percent",
                "values":[
                   83
                ]
             },
             {
                "name":"tstm",
                "levelType":"hl",
                "level":0,
                "unit":"percent",
                "values":[
                   0
                ]
             },
             {
                "name":"gust",
                "levelType":"hl",
                "level":10,
                "unit":"m/s",
                "values":[
                   4.0
                ]
             },
             {
                "name":"Wsymb2",
                "levelType":"hl",
                "level":0,
                "unit":"category",
                "values":[
                   6
                ]
             }
          ]
       },
       {
          "validTime":"2021-12-07T16:00:00Z",
          "parameters":[
             {
                "name":"spp",
                "levelType":"hl",
                "level":0,
                "unit":"percent",
                "values":[
                   -9
                ]
             },
             {
                "name":"pcat",
                "levelType":"hl",
                "level":0,
                "unit":"category",
                "values":[
                   0
                ]
             },
             {
                "name":"pmin",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"pmean",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"pmax",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"pmedian",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"tcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   8.0
                ]
             },
             {
                "name":"lcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   8
                ]
             },
             {
                "name":"mcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   2
                ]
             },
             {
                "name":"hcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   2.0
                ]
             },
             {
                "name":"t",
                "levelType":"hl",
                "level":2,
                "unit":"Cel",
                "values":[
                   -11.0
                ]
             },
             {
                "name":"msl",
                "levelType":"hmsl",
                "level":0,
                "unit":"hPa",
                "values":[
                   1017.4
                ]
             },
             {
                "name":"vis",
                "levelType":"hl",
                "level":2,
                "unit":"km",
                "values":[
                   50.0
                ]
             },
             {
                "name":"wd",
                "levelType":"hl",
                "level":10,
                "unit":"degree",
                "values":[
                   89
                ]
             },
             {
                "name":"ws",
                "levelType":"hl",
                "level":10,
                "unit":"m/s",
                "values":[
                   1.7
                ]
             },
             {
                "name":"r",
                "levelType":"hl",
                "level":2,
                "unit":"percent",
                "values":[
                   88
                ]
             },
             {
                "name":"tstm",
                "levelType":"hl",
                "level":0,
                "unit":"percent",
                "values":[
                   0
                ]
             },
             {
                "name":"gust",
                "levelType":"hl",
                "level":10,
                "unit":"m/s",
                "values":[
                   4.1
                ]
             },
             {
                "name":"Wsymb2",
                "levelType":"hl",
                "level":0,
                "unit":"category",
                "values":[
                   6
                ]
             }
          ]
       },
       {
          "validTime":"2021-12-07T17:00:00Z",
          "parameters":[
             {
                "name":"spp",
                "levelType":"hl",
                "level":0,
                "unit":"percent",
                "values":[
                   -9.0
                ]
             },
             {
                "name":"pcat",
                "levelType":"hl",
                "level":0,
                "unit":"category",
                "values":[
                   0
                ]
             },
             {
                "name":"pmin",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"pmean",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"pmax",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"pmedian",
                "levelType":"hl",
                "level":0,
                "unit":"kg/m2/h",
                "values":[
                   0.0
                ]
             },
             {
                "name":"tcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   8
                ]
             },
             {
                "name":"lcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   8
                ]
             },
             {
                "name":"mcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   1.0
                ]
             },
             {
                "name":"hcc_mean",
                "levelType":"hl",
                "level":0,
                "unit":"octas",
                "values":[
                   5
                ]
             },
             {
                "name":"t",
                "levelType":"hl",
                "level":2,
                "unit":"Cel",
                "values":[
                   -11.0
                ]
             },
             {
                "name":"msl",
                "levelType":"hmsl",
                "level":0,
                "unit":"hPa",
                "values":[
                   1017.2
                ]
             },
             {
                "name":"vis",
                "levelType":"hl",
                "level":2,
                "unit":"km",
                "values":[
                   8.0
                ]
             },
             {
                "name":"wd",
                "levelType":"hl",
                "level":10,
                "unit":"degree",
                "values":[
                   87
                ]
             },
             {
                "name":"ws",
                "levelType":"hl",
                "level":10,
                "unit":"m/s",
                "values":[
                   1.6
                ]
             },
             {
                "name":"r",
                "levelType":"hl",
                "level":2,
                "unit":"percent",
                "values":[
                   91
                ]
             },
             {
                "name":"tstm",
                "levelType":"hl",
                "level":0,
                "unit":"percent",
                "values":[
                   0.0
                ]
             },
             {
                "name":"gust",
                "levelType":"hl",
                "level":10,
                "unit":"m/s",
                "values":[
                   4.1
                ]
             },
             {
                "name":"Wsymb2",
                "levelType":"hl",
                "level":0,
                "unit":"category",
                "values":[
                   6
                ]
             }
          ]
       }

As you can see there is a parameters list inside of the timeSeries dictionary. I am trying to get the pcatvalue for every time. I have managed to get the list from the dictionary but not the specific value from the list. I have been trying in many ways, such as for loops and other.

The code I currently have is

import urllib
from urllib import request 
import json
import argparse
from time import strftime, localtime
from datetime import datetime, timedelta
import numpy as np


debug = False

hours = (datetime.now()   timedelta(hours=1)).strftime("%Y-%m-%dT%H:00:00Z")
print(hours)
longitude=12
latitude=58

if debug:
    print(hours, longitude, latitude)

url='http://opendata-download-metfcst.smhi.se/api/category/pmp1g/version/1/geopoint/lat/' str(latitude) '/lon/' str(longitude) '/data.json'
url='https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/' str(longitude) '/lat/' str(latitude) '/data.json'
print(url)
if debug:
    print(url)  
j = urllib.request.urlopen(url)

j_obj = json.load(j)

j_objt=j_obj['timeSeries']


new=[]

for i in j_objt:
 
    print(i)


This gives me the following output:

{'validTime': '2021-12-10T08:00:00Z', 'parameters': [{'name': 'spp', 'levelType': 'hl', 'level': 0, 'unit': 'percent', 'values': [-9]}, {'name': 'pcat', 'levelType': 'hl', 'level': 0, 'unit': 'category', 'values': [0]}, {'name': 'pmin', 'levelType': 'hl', 'level': 0, 'unit': 'kg/m2/h', 'values': [0.0]}, {'name': 'pmean', 'levelType': 'hl', 'level': 0, 'unit': 'kg/m2/h', 'values': [0.0]}, {'name': 'pmax', 'levelType': 'hl', 'level': 0, 'unit': 'kg/m2/h', 'values': [0.0]}, {'name': 'pmedian', 'levelType': 'hl', 'level': 0, 'unit': 'kg/m2/h', 'values': [0.0]}, {'name': 'tcc_mean', 'levelType': 'hl', 'level': 0, 'unit': 'octas', 'values': [8]}, {'name': 'lcc_mean', 'levelType': 'hl', 'level': 0, 'unit': 'octas', 'values': [6]}, {'name': 'mcc_mean', 'levelType': 'hl', 'level': 0, 'unit': 'octas', 'values': [2]}, {'name': 'hcc_mean', 'levelType': 'hl', 'level': 0, 'unit': 'octas', 'values': [6]}, {'name': 't', 'levelType': 'hl', 'level': 2, 'unit': 'Cel', 'values': [-0.3]}, {'name': 'msl', 'levelType': 'hmsl', 'level': 0, 'unit': 'hPa', 'values': [1002.4]}, {'name': 'vis', 'levelType': 'hl', 'level': 2, 'unit': 'km', 'values': [2.3]}, {'name': 'wd', 'levelType': 'hl', 'level': 10, 'unit': 'degree', 'values': [78]}, {'name': 'ws', 'levelType': 'hl', 'level': 10, 'unit': 'm/s', 'values': [1.6]}, {'name': 'r', 'levelType': 'hl', 'level': 2, 'unit': 'percent', 'values': [98]}, {'name': 'tstm', 'levelType': 'hl', 'level': 0, 'unit': 'percent', 'values': [0]}, {'name': 'gust', 'levelType': 'hl', 'level': 10, 'unit': 'm/s', 'values': [4.0]}, {'name': 'Wsymb2', 'levelType': 'hl', 'level': 0, 'unit': 'category', 'values': [4]}]}

I cant seem excract the pcat value from the list.

In conclusion, the desired output is:

2021-12-10T08:00:00Z
pcat: 0

Thank you in advance!

CodePudding user response:

You'll need to filter the parameters by name

for record in j_objt:
    param_pcat = list(filter(lambda p: p['name'] == 'pcat', record['parameters']))[0]
    print('validTime:', record['validTime'])
    print('pcat:', param_pcat['values'][0])

result:

validTime: 2021-12-07T14:00:00Z
pcat: 0
validTime: 2021-12-07T15:00:00Z
pcat: 0
validTime: 2021-12-07T16:00:00Z
pcat: 0
validTime: 2021-12-07T17:00:00Z
pcat: 0

CodePudding user response:

add a filter to retrieve elements in the list with pcat name

parameters=j_objt['parameters']
list = filter(lambda c: c['name'] == "pcat" , parameters);

print(j_objt['validTime']);
print("pcat:", list[0]['level']);
  • Related