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.

1
  • Look into who to tree walk a nested object. Commented Aug 9, 2021 at 21:36

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

2 Comments

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 If pm25 isn't in any of the objects, found will be set to undefined instead of throwing an error.
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
}

1 Comment

Using this soolution, it works for my needs. I do like seeing the if/else handling.

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.