3

I am banging my head trying to figure this out. And it should not be this hard. I am obviously missing a step.

I am pulling data from: openaq.org

The object I get back is based on a JSON object.
For now, I am using jQuery to parse the object and I am getting to the sub portion of the object that hold the specific parameter I want but I can't get to the specific key,value pair.

The object does not come back in the same order all the time. So when I tried to originally set up my call I did something like obj.results.measurements[0].

Well since the obj can come back in an random order, I went back to find the key,value pair again and it was the wrong value, throwing my visual off.

That said, I have looked at use jQuery's find() on JSON object and for some reason can not get what I need from the object I am given by openaq.org.

One version of the object looks like this:

{"meta":{"name":"openaq-api","license":"CC BY 4.0d","website":"https://u50g7n0cbj.execute-api.us-east-1.amazonaws.com/","page":1,"limit":100,"found":1},"results":[{"location":"Metro Lofts","city":null,"country":"US","coordinates":{"latitude":39.731,"longitude":-104.9888},"measurements":[{"parameter":"pm10","value":49.9,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"µg/m³"},{"parameter":"pm1","value":24,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"µg/m³"},{"parameter":"um100","value":0,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"particles/cm³"},{"parameter":"um025","value":0.28,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"particles/cm³"},{"parameter":"um010","value":4.1,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"particles/cm³"},{"parameter":"pm25","value":41.1,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"µg/m³"}]}]}

I am trying to get the "pm25" value.

The code I have tried is this:

function getAirQualityJson(){
    $.ajax({
      url: 'https://api.openaq.org/v2/latest?coordinates=39.73915,-104.9847',
      type: 'GET',
      dataType: "json"
     // data: data ,
  }).done(function(json){
 console.log("the json is" + JSON.stringify(json));
 console.log("the json internal is" + JSON.stringify(json.results));
 var obj = json.results;
 var pm25 = "";
 //console.log(JSON.stringify(json.results.measurements[0]["parameter"]));
 $.each(json.results[0], function(i,items){
   //console.log("obj item:" + JSON.stringify(obj[0].measurements));
    $.each(obj[0].measurements, function(y,things){
    //console.log("each measurement:" + JSON.stringify(obj[0].measurements[0].value));//get each measurement
    //pm 2.5
    //Can come back in random order, get value from the key "pm25" 
    // pm25 = JSON.stringify(obj[0].measurements[2].value);
    pm25 = JSON.stringify(obj[0].measurements[0].value); 

console.log("pm25 is: " + pm25); // not right
  });
});

//Trying Grep and map below too.  Not working
jQuery.map(obj, function(objThing) 
{ console.log("map it 1:" + JSON.stringify(objThing.measurements.parameter));
  if(objThing.measurements.parameter === "pm25"){
     //  return objThing; // or return obj.name, whatever.
       console.log("map it:" + objThing);
  }else{

    console.log("in else for pm25 map");
  }
});


jQuery.grep(obj, function(otherObj) {
  //return otherObj.parameter === "pm25";
  console.log("Grep it" + otherObj.measurements.parameter === "pm25");
});


});
}
getAirQualityJson();

https://jsfiddle.net/7quL0asz/

The loop is running through I as you can see I tried [2] which was the original placement of the 'pm25' value but then it switched up it's spot to the 3rd or 4th spot, so it is unpredictable.

I tried jQuery Grep and Map but it came back undefined or false.

So my question is, how would I parse this to get the 'pm25' key,value. After that, I can get the rest if I need them.

Thank you in advance for all the help.

Dominik
  • 6,078
  • 8
  • 37
  • 61
ClosDesign
  • 3,894
  • 9
  • 39
  • 62

2 Answers2

3

You can use array#find and optional chaining to do this,

because we are using optional chaining, undefined will be returned if a property is missing.

Demo:

let data = {"meta":{"name":"openaq-api","license":"CC BY 4.0d","website":"https://u50g7n0cbj.execute-api.us-east-1.amazonaws.com/","page":1,"limit":100,"found":1},"results":[{"location":"Metro Lofts","city":null,"country":"US","coordinates":{"latitude":39.731,"longitude":-104.9888},"measurements":[{"parameter":"pm10","value":49.9,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"µg/m³"},{"parameter":"pm1","value":24,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"µg/m³"},{"parameter":"um100","value":0,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"particles/cm³"},{"parameter":"um025","value":0.28,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"particles/cm³"},{"parameter":"um010","value":4.1,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"particles/cm³"},{"parameter":"pm25","value":41.1,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"µg/m³"}]}]}

let found = data?.results?.[0]?.measurements?.find?.(
    ({ parameter }) => parameter === "pm25"
);

console.log(found);
lejlun
  • 4,140
  • 2
  • 15
  • 31
  • 1
    What is this beautiful voodoo. That is awesome! How would this handle the parameter comes back empty. LIke if "pm25" is not in the object? – ClosDesign Aug 09 '21 at 21:56
  • @ClosDesign If `pm25` isn't in any of the objects, `found` will be set to undefined instead of throwing an error. – lejlun Aug 10 '21 at 10:53
2

You can iterate over measurements and find the object you need:

const data = '{"meta":{"name":"openaq-api","license":"CC BY 4.0d","website":"https://u50g7n0cbj.execute-api.us-east-1.amazonaws.com/","page":1,"limit":100,"found":1},"results":[{"location":"Metro Lofts","city":null,"country":"US","coordinates":{"latitude":39.731,"longitude":-104.9888},"measurements":[{"parameter":"pm10","value":49.9,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"µg/m³"},{"parameter":"pm1","value":24,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"µg/m³"},{"parameter":"um100","value":0,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"particles/cm³"},{"parameter":"um025","value":0.28,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"particles/cm³"},{"parameter":"um010","value":4.1,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"particles/cm³"},{"parameter":"pm25","value":41.1,"lastUpdated":"2021-08-09T20:49:38+00:00","unit":"µg/m³"}]}]}';
const json = JSON.parse(data);
let value = null;

const measurements = json?.results?.[0]?.measurements ?? null;
if(measurements) 
  for (const item of measurements) 
    if (item.parameter === 'pm25') {
      value = item.value;
      break;
    }

if (value) {
  // here you can use the value
  console.log(value);
}
else {
  // here you should handle the case where 'pm25' is not found
}
Amir MB
  • 3,233
  • 2
  • 10
  • 16