0

I have a multi-level JSON file I am trying to get a specific value out of, but everything I have tried just does not seem to work.

JSON:

   {  
   "mapVersion":59,
   "mapVersionString":"Fixed \"Me\" button. Fixed live updating of markers, Improved map performance...",
   "results":[  
      {  
         "ID":3644,
         "ParentID":null,
         "THINGSPEAK_PRIMARY_ID":"340896",
         "THINGSPEAK_PRIMARY_ID_READ_KEY":"ZGHNWCPCHD3HCNQM",
         "Label":"PurpleAir - Copperwood",
         "Lat":33.587254,
         "Lon":-112.196945,
         "PM2_5Value":"30.93",
         "State":null,
         "Type":"PMS5003+PMS5003+BME280",
         "Hidden":"false",
         "Flag":null,
         "DEVICE_LOCATIONTYPE":"outside",
         "DEVICE_BRIGHTNESS":"15",
         "isOwner":0,
         "A_H":null,
         "temp_f":"59",
         "humidity":"27",
         "pressure":"978.36",
         "AGE":0,
         "THINGSPEAK_SECONDARY_ID":"340899",
         "THINGSPEAK_SECONDARY_ID_READ_KEY":"CUMUMPCSVJM1L47F",
         "LastSeen":1514524202,
         "Version":"2.49j",
         "LastUpdateCheck":1514522192,
         "Uptime":"81220",
         "RSSI":"-64",
         "Stats":"{\"v\":30.93,\"v1\":32.82,\"v2\":34.24,\"v3\":33.03,\"v4\":20.49,\"v5\":22.6,\"v6\":21.8,\"pm\":30.93,\"lastModified\":1514524202452,\"timeSinceModified\":80183}"
      },
      {  
         "ID":3645,
         "ParentID":3644,
         "THINGSPEAK_PRIMARY_ID":"340901",
         "THINGSPEAK_PRIMARY_ID_READ_KEY":"VYY92GGQW8EAHU7F",
         "Label":"PurpleAir - Copperwood B",
         "Lat":33.587254,
         "Lon":-112.196945,
         "PM2_5Value":"31.63",
         "State":null,
         "Type":null,
         "Hidden":"false",
         "Flag":null,
         "DEVICE_LOCATIONTYPE":null,
         "DEVICE_BRIGHTNESS":null,
         "isOwner":0,
         "A_H":null,
         "temp_f":"59",
         "humidity":"27",
         "pressure":"978.39",
         "AGE":0,
         "THINGSPEAK_SECONDARY_ID":"340903",
         "THINGSPEAK_SECONDARY_ID_READ_KEY":"D5WDUE4DEKX6RNVW",
         "LastSeen":1514524232,
         "Version":"2.49j",
         "LastUpdateCheck":null,
         "Uptime":"81250",
         "RSSI":"-64",
         "Stats":"{\"v\":31.63,\"v1\":32.95,\"v2\":34.32,\"v3\":32.98,\"v4\":20.3,\"v5\":22.58,\"v6\":21.46,\"pm\":31.63,\"lastModified\":1514524232224,\"timeSinceModified\":79975}"
      }
   ]
}

I'm trying to reach the "v" value under Stats, but I'm at a loss on how to do it. I've tried json_decode into $json_result->results->stats; but all I get is a blank return, so I am at a loss what I am doing wrong (or not doing).

Any help is always a welcome learning oppurtunity!

2 Answers 2

1

results contains two array entries, and Stats is case sensitive.

php > var_dump($json_result->results[0]->Stats);
string(141) "{"v":30.93,"v1":32.82,"v2":34.24,"v3":33.03,"v4":20.49,"v5":22.6,"v6":21.8,"pm":30.93,"lastModified":1514524202452,"timeSinceModified":80183}"

Additionally, as you can see, Stats contains JSON, which will need to be decoded:

php > $json_result->results[0]->Stats = json_decode($json_result->results[0]->Stats);
php > var_dump($json_result->results[0]->Stats);
object(stdClass)#4 (10) {
  ["v"]=>
  float(30.93)
  ["v1"]=>
  float(32.82)
  ["v2"]=>
  float(34.24)
  ["v3"]=>
  float(33.03)
  ["v4"]=>
  float(20.49)
  ["v5"]=>
  float(22.6)
  ["v6"]=>
  float(21.8)
  ["pm"]=>
  float(30.93)
  ["lastModified"]=>
  int(1514524202452)
  ["timeSinceModified"]=>
  int(80183)
}
php > var_dump($json_result->results[0]->Stats->v);
float(30.93)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sorak - I did not even see that it did. I learned something today!
1

Try this one. you are missing result[0] index number

$json_result=json_decode($a);
echo "<pre>";
print_r($json_result->results[0]->Stats);

Check your desired output

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.