Unable to get object value is JSON parsed body
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 02:15 PM
On my PDI I have created a rest message to retrieve some weather details. The REST message is fine and the response body is what I expected. However, when to trying to get an object key value from the parsed JSON body. It will show up as "undefined" which I'm not sure why it is occurring.
BELOW IS THE START OF THE BODY
{
"@context": [
"https://geojson.org/geojson-ld/geojson-context.jsonld",
{
"@version": "1.1",
"wx": "https://api.weather.gov/ontology#",
"@vocab": "https://api.weather.gov/ontology#"
}
],
"type": "FeatureCollection",
"features": [
I have tried to do the following in a background script to try and get the value:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 04:11 PM
Hi there,
Some things to try:
- I dont think you need to stringify/parse it again, depending on the endpoint of course. But geojson would typically come in as a valid json already.
- Try only removing the stringify, as if its not coming in as a JSON, you are then doubly stringifying it which can cause issues.
- Dont use global.JSON.xxx, but just JSON.xxx
It would help if you could show me what endpoint you are calling. Or could you add:
gs.info(JSON.stringify(responseBody, null, 4))
before the parsing part and send back the result of it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 05:26 PM
Here is the website API which doesn't require authentication/credentials:
https://www.weather.gov/documentation/services-web-api#/
For the GET method endpoint was --> https://api.weather.gov/alerts/
I used the GET /alerts api (https://www.weather.gov/documentation/services-web-api#/default/alerts_query)
When i used the gs.info(JSON.stringify(responseBody, null, 4)) here is what i got below:
"{\n \"@context\": [\n \"https://geojson.org/geojson-ld/geojson-context.jsonld\",\n {\n \"@version\": \"1.1\",\n \"wx\": \"https://api.weather.gov/ontology#\",\n \"@vocab\": \"https://api.weather.gov/ontology#\"\n }\n ],\n \"type\": \"FeatureCollection\",\n \"features\": [\n {\n \"id\": \"https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.6058fcd615c15870fceae810fce285f43dc0c522.005.1...",\n \"type\": \"Feature\",\n \"geometry\": null,\n \"properties\": {\n \"@id\": \"https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.6058fcd615c15870fceae810fce285f43dc0c522.005.1...",\n \"@type\": \"wx:Alert\",\n \"id\": \"urn:oid:2.49.0.1.840.0.6058fcd615c15870fceae810fce285f43dc0c522.005.1\",\n \"areaDesc\": \"Cape Cleare to Gore Point out to 15 NM\",\n \"geocode\": {\n \"SAME\": [\n \"058715\"\n ],\n \"UGC\": [\n \"PKZ715\"\n ]\n },\n \"affectedZones\": [\n \"https://api.weather.gov/zones/forecast/PKZ715\"\n ],\n \"references\": [],\n \"sent\": \"2024-09-05T15:31:00-08:00\",\n \"effective\": \"2024-09-05T15:31:00-08:00\",\n \"onset\": \"2024-09-05T17:00:00-08:00\",\n \"expires\": \"2024-09-06T04:45:00-08:00\",\n \"ends\": \"2024-09-06T05:00:00-08:00\",\n \"status\": \"Actual\",\n \"messageType\": \"Alert\",\n \"category\": \"Met\",\n \"severity\": \"Minor\",\n \"certainty\": \"Likely\",\n \"urgency\": \"Expected\",\n \"event\": \"Small Craft Advisory\",\n \"sender\": \"w-nws.webmaster@noaa.gov\",\n \"senderName\": \"NWS Anchorage AK\",\n \"headline\": \"Small Craft Advisory issued September 5 at 3:31PM AKDT until September 6 at 5:00AM AKDT by NWS Anchorage AK\",\n \"description\": \"Coastal Waters Forecast for the Northern Gulf of Coast\\nup to 100 nm out including Kodiak Island and Cook Inlet.\\n\\n
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 07:09 PM
Hi @TCole94
Please try this script, I'm seeing issue in parsing the script.
Please mark helpful and encourage us!
Rampriya S