user12251763
user12251763

Reputation:

how to print a specific part of a json file in python?

im new to python and i would like to know how a can print the specific data between the curly brackets in a json file. There are a lot of curly brackets, but i want the data between one pair of brackets. Here is a part of a the json:

  "actual": {
    "$id": "3",
    "actualradarurl": "https://api.buienradar.nl/image/1.0/RadarMapNL?w=500&h=512",
    "sunrise": "2019-10-21T08:14:00",
    "sunset": "2019-10-21T18:34:00",
    "stationmeasurements": [
      {
        "$id": "4",
        "stationid": 6391,
        "stationname": "Meetstation Arcen",
        "lat": 51.5,
        "lon": 6.2,
        "regio": "Venlo",
        "timestamp": "2019-10-21T14:30:00",
        "weatherdescription": "Zwaar bewolkt",
        "iconurl": "https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png",
        "graphUrl": "https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c",
        "winddirection": "ZW",
        "temperature": 14.4,
        "groundtemperature": 14.4,
        "feeltemperature": 13.5,
        "windgusts": 7.6,
        "windspeed": 3.8,
        "windspeedBft": 3,
        "humidity": 72.0,
        "precipitation": 0.0,
        "sunpower": 100.0,
        "rainFallLast24Hour": 3.2,
        "rainFallLastHour": 0.0,
        "winddirectiondegrees": 214
      },
      {
        "$id": "5",
        "stationid": 6275,
        "stationname": "Meetstation Arnhem",
        "lat": 52.07,
        "lon": 5.88,
        "regio": "Arnhem",
        "timestamp": "2019-10-21T14:30:00",
        "weatherdescription": "Zwaar bewolkt",
        "iconurl": "https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png",
        "graphUrl": "https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c",
        "winddirection": "ZZW",
        "airpressure": 1016.2,
        "temperature": 14.9,
        "groundtemperature": 15.3,
        "feeltemperature": 13.1,
        "visibility": 47200.0,
        "windgusts": 11.3,
        "windspeed": 7.5,
        "windspeedBft": 4,
        "humidity": 74.0,
        "precipitation": 0.0,
        "sunpower": 303.0,
        "rainFallLast24Hour": 1.9,
        "rainFallLastHour": 0.0,
        "winddirectiondegrees": 197
      },

Upvotes: 0

Views: 2286

Answers (2)

Yaroslav  Kornachevskyi
Yaroslav Kornachevskyi

Reputation: 1218

import json

with open('data.txt') as json_file:
    data = json.load(json_file)
    print(data['actual']['stationmeasurements'][0])

For first set of curlies (idx = 0) it prints:

{'$id': '4', 'stationid': 6391, 'stationname': 'Meetstation Arcen', 'lat': 51.5, 'lon': 6.2, 'regio': 'Venlo', 'timestamp': '2019-10-21T14:30:00', 'weatherdescription': 'Zwaar bewolkt', 'iconurl': 'https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png', 'graphUrl': 'https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c', 'winddirection': 'ZW', 'temperature': 14.4, 'groundtemperature': 14.4, 'feeltemperature': 13.5, 'windgusts': 7.6, 'windspeed': 3.8, 'windspeedBft': 3, 'humidity': 72.0, 'precipitation': 0.0, 'sunpower': 100.0, 'rainFallLast24Hour': 3.2, 'rainFallLastHour': 0.0, 'winddirectiondegrees': 214}

Upvotes: 0

JSON files can be loaded as python dictionaries. So basically, what you want is to load the JSON as a dict object and print a given field of said object

import json
x :str = "{ 'key1': 'value1', 'key2': { 'subkey1': 'subval'} }"
dict_object = json.loads(x)
print(x['key2']) # Prints the dict {subkey1: subval}
print(x['key2']['subkey1']) # prints subval

Upvotes: 1

Related Questions