SmartThings Community

Interpreting JSON data

Hi there folks

I’m ashamed to be posting such a Noob question so apologies in advance.

I am trying to get some data from an API and present it in SmartThings. What should be easy is proving to be anything but for my limited coding ability.

I’m making a call to https://api.carbonintensity.org.uk/regional/postcode/GL51 in order to dig out the real-time energy intensity of power generation in my area of the UK. This returns:

{“data”:[{“regionid”:7,“dnoregion”:“WPD West Midlands”,“shortname”:“West Midlands”,“postcode”:“GL51”,“data”:[{“from”:“2019-04-03T17:00Z”,“to”:“2019-04-03T17:30Z”,“intensity”:{“forecast”:279,“index”:“high”},“generationmix”:[{“fuel”:“biomass”,“perc”:3.8},{“fuel”:“coal”,“perc”:5.9},{“fuel”:“imports”,“perc”:0.2},{“fuel”:“gas”,“perc”:55.4},{“fuel”:“nuclear”,“perc”:3.9},{“fuel”:“other”,“perc”:0},{“fuel”:“hydro”,“perc”:6.6},{“fuel”:“solar”,“perc”:3.7},{“fuel”:“wind”,“perc”:20.5}]}]}]}

which is all very pretty and contains EXACTLY what I want.

Some time ago I wrote a WebCORE piece of code and was interested in the index value at the time. I simply wanted to control some switches to come on when carbon intensity is LOW or VERY LOW. In WebCORE I used $response.data.data.intensity.index and this did the trick.

In SmartApp I added:

log.debug “response data: {resp.data}" log.debug "intensity index: {resp.data.data.intensity.index}”

but I get:

98cfdccc-a158-42b2-adc9-49d2d609c8bf 10:18:01: debug intensity index:
98cfdccc-a158-42b2-adc9-49d2d609c8bf 10:18:01: debug response data: [data:[[data:[[from:2019-04-04T08:30Z, generationmix:[[fuel:biomass, perc:3.4], [fuel:coal, perc:5.2], [fuel:imports, perc:0.8], [fuel:gas, perc:51.8], [fuel:nuclear, perc:11.3], [fuel:other, perc:0], [fuel:hydro, perc:7.8], [fuel:solar, perc:1.6], [fuel:wind, perc:18.1]], intensity:[forecast:256, index:moderate], to:2019-04-04T09:00Z]], dnoregion:WPD West Midlands, postcode:GL51, regionid:7, shortname:West Midlands]]]

How can I start breaking this data out in a way that I can use?

TIA

Use JsonSlurper(). You can search Google for examples. Here is another thread with details too:

Thanks very much.

‘‘Twas late last night and I thought that I had Googled :slight_smile:

1 Like