jc72
jc72

Reputation: 203

How would I catch and deal with "undefined" from parsed json object

I'm trying to populate a spreadsheet from a RESTful service and sometimes there's no such property so I get an "undefined" in my spreadsheet and the script stops and gives an error. I have no clue as to how to go about dealing with it, or skipping over the "undefined" part.

Here's a sample query in json

{

"rel": "self",
"url": "https://www.sciencebase.gov/catalog/items?max=2&s=Search&q=project+
2009+WLCI&format=json&fields=title%2Csummary%2Cspatial%2Cfacets",
"total": 65,
"nextlink": {
    "rel": "next",
    "url": "https://www.sciencebase.gov/catalog/items?max=2&s=
Search&q=project+2009+WLCI&format=json&fields=title%2Csummary%2
Cspatial%2Cfacets&offset=2"
},
"items": [
    {
        "link": {
            "rel": "self",
            "url": "https://www.sciencebase.gov/catalog/item/
             4f4e4ac3e4b07f02db67875f"
        },
        "id": "4f4e4ac3e4b07f02db67875f",
        "title": "Decision-Making and Evaluation - Social and Economic 
        Evaluation Supporting Adaptive Management for WLCI",
        "summary": "Provide information on the social and economic environment 
        for the WLCI area and provide context for the biological and physical 
        aspects of this project.",
        "spatial": {
            "representationalPoint": [
                -108.585,
                42.141
            ]
        },
        "facets": [
            {
                "startDate": "2007-10-01 00:00:00",
                "projectStatus": "Active",
                "facetName": "Project",
                "_class": "ProjectFacet",
                "active": true,
                "className": "gov.sciencebase.catalog.item.facet.ProjectFacet",
                "endDate": null,
                "projectType": "Science",
                "_embeddedClassName": "gov.sciencebase.catalog.item.facet.
                ProjectFacet"
            }
        ]
    },
    {
        "link": {
            "rel": "self",
            "url": "https://www.sciencebase.gov/catalog/item
            /4f4e4ac0e4b07f02db676d57"
        },
        "id": "4f4e4ac0e4b07f02db676d57",
        "title": "Data and Information Management Products for the Wyoming 
        Landscape Conservation Initiative"
    }
]

}

Since the second item has only a title and nothing more after that I get "undefined" if I try to get a summary or facet etc. for items using a loop. I've thought of and tried using if statements like

if (parsedResponse.items[i].summary === "undefined") {
    Do something here;
}

but this doesn't seem to work. Any suggestions I could try are appreciated. Thanks

Upvotes: 5

Views: 27311

Answers (3)

tarun
tarun

Reputation: 1

  function getLastNonBlankColCrow(sheet,columnName, maxi) {
  var lastNonBlankColCrow = 0;
  var rangeded = sheet.getRange(columnName+1+":"+columnName+maxi).getValues();
  for (var i=0;i<=maxi; i++)
    {
        if (rangeded[i] == undefined)
         {
         }
      else
     {
       if ( rangeded[i][0]!="" ) 
        { 
        lastNonBlankColCrow = i+1;
         }
      }
   }
   return lastNonBlankColCrow;
  }

Upvotes: 0

megabyte1024
megabyte1024

Reputation: 8660

A solution is

if (parsedResponse.items[i].summary == undefined) {
    Do something here;
}

or

if (parsedResponse.items[i].summary == null) {
    Do something here;
}

Upvotes: 12

Prasanth
Prasanth

Reputation: 5258

This should work:

if (typeof(parsedResponse.items[i].summary) === "undefined") {

Upvotes: 1

Related Questions