Trouble with complex JSON

Hello, I have the following JSON:

{
  "cod": "200",
  "message": 0,
  "cnt": 40,
  "list": [
    {
      "dt": 1609189200,
      "main": {
        "temp": 281.34,
        "feels_like": 276.87,
        "temp_min": 280.51,
        "temp_max": 281.34,
        "pressure": 1020,
        "sea_level": 1020,
        "grnd_level": 1019,
        "humidity": 63,
        "temp_kf": 0.83
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03d"
        }
      ],
      "clouds": {
        "all": 28
      },
      "wind": {
        "speed": 3.9,
        "deg": 240
      },
      "visibility": 10000,
      "pop": 0,
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2020-12-28 21:00:00"
    },
    {
      "dt": 1609200000,
      "main": {
        "temp": 279.82,
        "feels_like": 275.24,
        "temp_min": 279.18,
        "temp_max": 279.82,
        "pressure": 1021,
        "sea_level": 1021,
        "grnd_level": 1020,
        "humidity": 70,
        "temp_kf": 0.64
      },
      "weather": [
        {
          "id": 801,
          "main": "Clouds",
          "description": "few clouds",
          "icon": "02n"
        }
      ],
      "clouds": {
        "all": 15
      },
      "wind": {
        "speed": 4.06,
        "deg": 225
      },
      "visibility": 10000,
      "pop": 0,
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2020-12-29 00:00:00"
    },
truncated

I’m trying to get the value of the second weather main, “Clouds”. I’ve tried:

$json["list"][1][0]["weather"]["main"];
$json["list"][1]["weather"]["main"];

but neither have worked. What would be the right answer?

1 Like

Basically you go from top to bottom and see which key you need to drill down to the next level:

  1. We need a value from key "list"
  2. "list" contains an array, we want the first element, with key 0
  3. From this array, we want "weather"
  4. "weather" contains an array; from this array we’re interested in the first (and only) element, again, key 0
  5. We’re interested in key "main" of this array

All together, $json["list"][0]['weather][0]['main']

1 Like

Interesting. I always go the other way. Find the value i need to get, and walk back to the top of the tree. (Either approach is fine, btw, i’m not suggesting anyone’s wrong here. Just different ways of looking at things.)

1 Like

Interesting, never thought of it that way.

But you still need to write it from top to bottom (left to right) to access it so I am not sure I get the point. If you wrote it bottom up you would have to keep clicking the cursor to the start of the line. Is that how you are doing it?

Pretty much, yup.

$json["main"]
$json["weather"][0]["main"]
$json["list"][0]["weather"][0]["main"]

(I’ve found the habit of writing array-index along with the name of the array at the same time.)

I also find it invaluably helpful with actually complex/large JSON files to push it through a visualizer/treeview generator to make it easier to examine the tree steps. Something like http://jsonviewer.stack.hu/

1 Like

Cool JSON tool! :+1:

1 Like