Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 613
jc72 Avatar asked Dec 15 '22 21:12

jc72


1 Answers

A solution is

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

or

if (parsedResponse.items[i].summary == null) {
    Do something here;
}
like image 125
megabyte1024 Avatar answered Dec 28 '22 05:12

megabyte1024