Need simple example code:

Great find btw!

So based on this I believe I should be able to see data from response.data.features.properties.sent but I still keep getting the error. This is where I get stuck most times attempting to get the data. Once I can get the data I can be fairly proficient in coding the logic.

I would try changing your code as follows:

def requestParams =
[
	uri:  wxURI,
	requestContentType: "application/json",
	contentType: "application/json"
]

EDIT: Added missing comma above.

Well that did something interesting. I now do not have the error but it is null for the response. I copied the response.data that is in the logs and attempted to place into your online JSON viewer tool you introduced me to but now it is not formatted JSON.

Here is my logging output:

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.599 pm [info](http://10.0.2.38/device/edit/1903)type: null

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.597 pm [info](http://10.0.2.38/device/edit/1903)response data: [@context:[https://raw.githubusercontent.com/geojson/geojson-ld/master/contexts/geojson-base.jsonld, [wx:https://api.weather.gov/ontology#, @vocab:https://api.weather.gov/ontology#]], type:FeatureCollection, features:[[id:https://api.weather.gov/alerts/NWS-IDP-PROD-3381285-2960502, type:Feature, geometry:null, properties:[@id:https://api.weather.gov/alerts/NWS-IDP-PROD-3381285-2960502, @type:wx:Alert, id:NWS-IDP-PROD-3381285-2960502, areaDesc:Lake; Kendall; Porter; Will; Grundy, geocode:[UGC:[INZ001, ILZ020, INZ002, ILZ022, ILZ021], SAME:[018089, 017093, 018127, 017197, 017063]], affectedZones:[https://api.weather.gov/zones/forecast/INZ001, https://api.weather.gov/zones/forecast/ILZ020, https://api.weather.gov/zones/forecast/INZ002, https://api.weather.gov/zones/forecast/ILZ022, https://api.weather.gov/zones/forecast/ILZ021], references:[[@id:https://api.weather.gov/alerts/NWS-IDP-PROD-3380703-2959958, identifier:NWS-IDP-PROD-3380703-2959958, sender:w-nws.webmaster@noaa.gov, sent:2019-02-19T03:35:00-06:00]], sent:2019-02-19T11:01:00-06:00, effective:2019-02-19T11:01:00-06:00, onset:2019-02-20T03:00:00-06:00, expires:2019-02-19T19:15:00-06:00, ends:2019-02-20T12:00:00-06:00, status:Actual, messageType:Update, category:Met, severity:Moderate, certainty:Likely, urgency:Expected, event:Winter Weather Advisory, sender:w-nws.webmaster@noaa.gov, senderName:NWS Chicago IL, headline:Winter Weather Advisory issued February 19 at 11:01AM CST expiring February 20 at 12:00PM CST by NWS Chicago IL, description:* WHAT...Mixed precipitation expected. Total snow accumulations of up to one inch and ice accumulations of a few hundredths leading to at least a light glaze expected. * WHERE...In Indiana, Lake IN and Porter Counties. In Illinois, Kendall, Grundy and Will Counties. * WHEN...From 3 AM to noon CST Wednesday. * ADDITIONAL DETAILS...Plan on slippery road conditions. The hazardous conditions could impact the morning commute., instruction:A Winter Weather Advisory means that periods of snow, sleet or freezing rain will cause travel difficulties. Expect slippery roads and limited visibilities, and use caution while driving. The latest road conditions can be obtained by going to www.gettingaroundillinois.com in Illinois or by calling 1 8 0 0 2 6 1 7 6 2 3 in Indiana., response:Execute, parameters:[NWSheadline:[WINTER WEATHER ADVISORY REMAINS IN EFFECT FROM 3 AM TO NOON CST WEDNESDAY], HazardType:[Mixed Precipitation], VTEC:[/O.CON.KLOT.WW.Y.0014.190220T0900Z-190220T1800Z/], PIL:[LOTWSWLOT], BLOCKCHANNEL:[CMAS, EAS, NWEM], eventEndingTime:[2019-02-20T12:00:00-06:00]]]]], title:current watches, warnings, and advisories for 41.540583 N, 88.276465 W, updated:2019-02-19T11:15:00-10:00]

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.528 pm [info](http://10.0.2.38/device/edit/1903)response contentType: application/geo+json

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.527 pm [info](http://10.0.2.38/device/edit/1903)params2: [uri:https://api.weather.gov/alerts/active?status=actual&point=41.540583%2C-88.276465, requestContentType:application/json, contentType:application/json]

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.525 pm [info](http://10.0.2.38/device/edit/1903)Response2: Strict-Transport-Security : max-age=31536000 ; includeSubDomains ; preload

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.523 pm [info](http://10.0.2.38/device/edit/1903)Response2: Vary : Accept

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.521 pm [info](http://10.0.2.38/device/edit/1903)Response2: Vary : Accept-Encoding

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.519 pm [info](http://10.0.2.38/device/edit/1903)Response2: Connection : keep-alive

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.518 pm [info](http://10.0.2.38/device/edit/1903)Response2: Date : Tue, 19 Feb 2019 21:16:10 GMT

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.516 pm [info](http://10.0.2.38/device/edit/1903)Response2: Expires : Tue, 19 Feb 2019 21:16:40 GMT

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.514 pm [info](http://10.0.2.38/device/edit/1903)Response2: Cache-Control : public, max-age=30, s-maxage=30

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.512 pm [info](http://10.0.2.38/device/edit/1903)Response2: X-Request-ID : 7327f7fc-6b94-498e-8b90-3e90dc3d2b05

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.509 pm [info](http://10.0.2.38/device/edit/1903)Response2: X-Correlation-ID : 7327f7fc-6b94-498e-8b90-3e90dc3d2b05

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.506 pm [info](http://10.0.2.38/device/edit/1903)Response2: X-Server-ID : vm-lnx-nids-apiapp10.ncep.noaa.gov

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.503 pm [info](http://10.0.2.38/device/edit/1903)Response2: Access-Control-Allow-Origin : *

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.500 pm [info](http://10.0.2.38/device/edit/1903)Response2: Last-Modified : Tue, 19 Feb 2019 21:15:00 GMT

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.496 pm [info](http://10.0.2.38/device/edit/1903)Response2: Content-Type : application/geo+json

[dev:1903](http://10.0.2.38/logs#dev1903)2019-02-19 03:16:10.493 pm [info](http://10.0.2.38/device/edit/1903)Response2: Server : nginx/1.12.2

Response.data
[@context:[https://raw.githubusercontent.com/geojson/geojson-ld/master/contexts/geojson-base.jsonld, [wx:https://api.weather.gov/ontology#, @vocab:https://api.weather.gov/ontology#]], type:FeatureCollection, features:[[id:https://api.weather.gov/alerts/NWS-IDP-PROD-3381285-2960502, type:Feature, geometry:null, properties:[@id:https://api.weather.gov/alerts/NWS-IDP-PROD-3381285-2960502, @type:wx:Alert, id:NWS-IDP-PROD-3381285-2960502, areaDesc:Lake; Kendall; Porter; Will; Grundy, geocode:[UGC:[INZ001, ILZ020, INZ002, ILZ022, ILZ021], SAME:[018089, 017093, 018127, 017197, 017063]], affectedZones:[https://api.weather.gov/zones/forecast/INZ001, https://api.weather.gov/zones/forecast/ILZ020, https://api.weather.gov/zones/forecast/INZ002, https://api.weather.gov/zones/forecast/ILZ022, https://api.weather.gov/zones/forecast/ILZ021], references:[[@id:https://api.weather.gov/alerts/NWS-IDP-PROD-3380703-2959958, identifier:NWS-IDP-PROD-3380703-2959958, sender:w-nws.webmaster@noaa.gov, sent:2019-02-19T03:35:00-06:00]], sent:2019-02-19T11:01:00-06:00, effective:2019-02-19T11:01:00-06:00, onset:2019-02-20T03:00:00-06:00, expires:2019-02-19T19:15:00-06:00, ends:2019-02-20T12:00:00-06:00, status:Actual, messageType:Update, category:Met, severity:Moderate, certainty:Likely, urgency:Expected, event:Winter Weather Advisory, sender:w-nws.webmaster@noaa.gov, senderName:NWS Chicago IL, headline:Winter Weather Advisory issued February 19 at 11:01AM CST expiring February 20 at 12:00PM CST by NWS Chicago IL, description:* WHAT...Mixed precipitation expected. Total snow accumulations
of up to one inch and ice accumulations of a few hundredths
leading to at least a light glaze expected.

* WHERE...In Indiana, Lake IN and Porter Counties. In Illinois,
Kendall, Grundy and Will Counties.

* WHEN...From 3 AM to noon CST Wednesday.

* ADDITIONAL DETAILS...Plan on slippery road conditions. The
hazardous conditions could impact the morning commute., instruction:A Winter Weather Advisory means that periods of snow, sleet or
freezing rain will cause travel difficulties. Expect slippery
roads and limited visibilities, and use caution while driving.

The latest road conditions can be obtained by going to
www.gettingaroundillinois.com in Illinois or by calling
1 8 0 0 2 6 1 7 6 2 3 in Indiana., response:Execute, parameters:[NWSheadline:[WINTER WEATHER ADVISORY REMAINS IN EFFECT FROM 3 AM TO NOON CST WEDNESDAY], HazardType:[Mixed Precipitation], VTEC:[/O.CON.KLOT.WW.Y.0014.190220T0900Z-190220T1800Z/], PIL:[LOTWSWLOT], BLOCKCHANNEL:[CMAS, EAS, NWEM], eventEndingTime:[2019-02-20T12:00:00-06:00]]]]], title:current watches, warnings, and advisories for 41.540583 N, 88.276465 W, updated:2019-02-19T11:15:00-10:00]

@aaron, you may have a typo somewhee in your code.
I loaded your driver with @ogiewon suggested change and Iwas able to do an initial parse. See full code below

import groovy.json.*
	
	metadata {
	definition (name: "NOAA Weather Alerts", namespace: "aaronward", author: "Aaron Ward") {
		capability "Refresh"
	}
	preferences {
		section {
			input (
				type: "bool",
				name: "enableDebugLogging",
				title: "Enable Debug Logging?",
				required: true,
				defaultValue: true
			)
		}
	}
}

def log(msg) {
	if (enableDebugLogging) {
		log.debug(msg)	
	}
}

def installed () {
	log.info "${device.displayName}.installed()"
    updated()
}

def updated () {
	log.info "${device.displayName}.updated()"
    runIn(5, refresh)				// But test it once, right after we install or update it too.
}

def refresh() {
		def wxURI = "https://api.weather.gov/alerts/active?status=actual&point=41.540583%2C-88.276465"

	def requestParams =
	[
		uri:  wxURI,
		requestContentType: "application/json",
		contentType: "application/json"
	]

	httpGet(requestParams)	{	  response ->
		if (response?.status == 200)
		{
			log.info response.data.features
			log.info response.data.features.id
		}
		else
		{
			log.warn "${response?.status}"
		}
	}

}

yielded this:

2019-02-19 04:22:59.975 pm [info](http://192.168.1.55/device/edit/194)[https://api.weather.gov/alerts/NWS-IDP-PROD-3381285-2960502]

1 Like

Looks like I left a comma out in my original code snippet post above... Sorry about that @aaron.

1 Like

Holy Json Batman. I've used json validators but nothing that formatted it so it was this easy to visually parse.
Bookmarking this one!!

1 Like

@ogiewon and @stephack

So I can get to response.data.features.id but you can't seem to parse anything further down. I need the following:

response.data.features.properties.sent
response.data.features.properties.description
response.data.features.properties.descArea

Thoughts?

Everything I say below is to the best of my understanding and I am probably using the wrong coding terminology, but I think it should help you understand how to get what you need going forward.

With the assistance of @ogiewon's great tool, it makes it a bit easier to see how to get what you want. Everything in the blue circle is listed under "features". Essentially "features" is an array that contains "title", "updated" and another subarray (circled in red) referenced at position "0".

Here's a couple of options to get you the feature/properties/id

log.debug response.data.features[0].properties.id

or

log.debug response.data.features[0]["properties"].id

image

Make sense?

2 Likes

This tool helped me a lot when developing the Logitech Harmony Hub webSockets based driver. I think I just got lucky in a Google search for “json viewer”.

1 Like

@stephack

You are awesome! Thank you for your guidance on this! It works!

2 Likes

I use this for generating selectors:

http://www.jsonquerytool.com/

1 Like

That's pretty nifty!

OK so now I know how to parse a single tree but....... what if I want to parse ALL the trees in 1 go ?

@ogiewon

You can take a look inside my Logitech Harmony Hub Parent Driver, as it iterates through json responses from the Harmony Hub to parse out the pieces it needs.

Here's a snippet of code that demonstrates iterating through json and pulling out the pieces I needed to take action on.

            json?.data?.activity?.each { it ->
                def tempID = (it.id == "-1") ? "PowerOff" : "${it.id}"                    
                if (logEnable) log.debug "Activity Label: ${it.label}, ID: ${tempID}"
                
                //store portion of config results in state veraible (needed for volume/channel control) 
                def volume = "null"
                if (it.roles?.VolumeActivityRole) volume = it.roles?.VolumeActivityRole
                def channel = "null"
                if (it.roles?.ChannelChangingActivityRole) channel = it.roles?.ChannelChangingActivityRole
                state.HarmonyConfig << ["id":"${it.id}", "label":"${it.label}", "VolumeActivityRole":"${volume}", "ChannelChangingActivityRole":"${channel}"]
                
                //Create a Child Switch Device for each Activity if needed, default all of them to 'off' for now
                updateChild(tempID, "unknown", it.label)
            }
1 Like

If you'd like an extreme example of json parsing look at echo speaks. It does some serious work.

1 Like

@aaron How did you go with getting this to work? I want to do something similar with our Australian Bureau of Meteorology that has a similar service where you send a http get and receive a JSON response. Here's an example URL for my home town. Thinking it'd be neat to access the current temp and play around with automation like closing our outdoor blinds if the temp is over a certain value or it's raining.

I created an entire app.

I will look at your given URL and see what is possible.

2 Likes

Thanks Aaron! That's brilliant! Thanks for having a look at it.

Good news - Australia has adopted the open standard CAP for weather alerts. SO there is a good possibility of creating a NOAA like application for AU citizens.

Bad news - all the documentation posted for CAP-AU does not contain the API URL to query and request weather alert data. I have sent an email of inquiry to see if I can acquire it from the AU meteorologist committee.

Keep you posted.