Struggling with decoding this JSON

Posted on
Sat Nov 09, 2019 3:14 pm
neilk offline
Posts: 715
Joined: Jul 13, 2015
Location: Reading, UK

Struggling with decoding this JSON

Hi,
I am working on my first plugin, and I have hit the end of my knowledge. I get the following json back, by doing

Code: Select all
status=requests.get(url)
alljson = json.loads(status.text)

Code: Select all
{u'info': {u'opt': 119, u'core': u'2_5_2', u'leds': {u'count': 151, u'rgbw': False, u'maxpwr': 850, u'pwr': 847, u'pin': [2], u'maxseg': 10}, u'udpport': 21324, u'ver': u'0.8.6', u'name': u'WLED', u'vid': 1910255, u'brand': u'WLED', u'palcount': 50, u'uptime': 164461, u'product': u'DIY light', u'live': False, u'btype': u'bin', u'fxcount': 83, u'freeheap': 20480, u'mac': u'2cf432779560', u'arch': u'esp8266'}, u'state': {u'on': True, u'nl': {u'on': False, u'dur': 60, u'tbri': 0, u'fade': True}, u'ps': -1, u'udpn': {u'recv': True, u'send': False}, u'transition': 650, u'seg': [{u'ix': 128, u'cln': -1, u'sx': 170, u'fx': 54, u'rev': False, u'stop': 151, u'len': 151, u'col': [[11, 22, 33], [0, 51, 255], [0, 0, 0]], u'start': 0, u'sel': True, u'pal': 8, u'id': 0}], u'bri': 255, u'pl': -1}, u'palettes': [u'Default', u'Random Cycle', u'Primary Color', u'Based on Primary', u'Set Colors', u'Based on Set', u'Party', u'Cloud', u'Lava', u'Ocean', u'Forest', u'Rainbow', u'Rainbow Bands', u'Sunset', u'Rivendell', u'Breeze', u'Red & Blue', u'Yellowout', u'Analogous', u'Splash', u'Pastel', u'Sunset 2', u'Beech', u'Vintage', u'Departure', u'Landscape', u'Beach', u'Sherbet', u'Hult', u'Hult 64', u'Drywet', u'Jul', u'Grintage', u'Rewhi', u'Tertiary', u'Fire', u'Icefire', u'Cyane', u'Light Pink', u'Autumn', u'Magenta', u'Magred', u'Yelmag', u'Yelblu', u'Orange & Teal', u'Tiamat', u'April Night', u'Orangery', u'C9', u'Sakura'], u'effects': [u'Solid', u'Blink', u'Breathe', u'Wipe', u'Wipe Random', u'Random Colors', u'Sweep', u'Dynamic', u'Colorloop', u'Rainbow', u'Scan', u'Dual Scan', u'Fade', u'Chase', u'Chase Rainbow', u'Running', u'Saw', u'Twinkle', u'Dissolve', u'Dissolve Rnd', u'Sparkle', u'Dark Sparkle', u'Sparkle+', u'Strobe', u'Strobe Rainbow', u'Mega Strobe', u'Blink Rainbow', u'Android', u'Chase', u'Chase Random', u'Chase Rainbow', u'Chase Flash', u'Chase Flash Rnd', u'Rainbow Runner', u'Colorful', u'Traffic Light', u'Sweep Random', u'Running 2', u'Red & Blue', u'Stream', u'Scanner', u'Lighthouse', u'Fireworks', u'Rain', u'Merry Christmas', u'Fire Flicker', u'Gradient', u'Loading', u'In Out', u'In In', u'Out Out', u'Out In', u'Circus', u'Halloween', u'Tri Chase', u'Tri Wipe', u'Tri Fade', u'Lightning', u'ICU', u'Multi Comet', u'Dual Scanner', u'Stream 2', u'Oscillate', u'Pride 2015', u'Juggle', u'Palette', u'Fire 2012', u'Colorwaves', u'BPM', u'Fill Noise', u'Noise 1', u'Noise 2', u'Noise 3', u'Noise 4', u'Colortwinkles', u'Lake', u'Meteor', u'Smooth Meteor', u'Railway', u'Ripple', u'Twinklefox', u'Twinklecat', u'Halloween Eyes']}

I then can extract just the status by
Code: Select all
statusjson = alljson.get("state")

Which returns
Code: Select all
{u'on': True, u'nl': {u'on': False, u'dur': 60, u'tbri': 0, u'fade': True}, u'ps': -1, u'udpn': {u'recv': True, u'send': False}, u'transition': 650, u'seg': [{u'ix': 128, u'cln': -1, u'sx': 170, u'fx': 54, u'rev': False, u'stop': 151, u'len': 151, u'col': [[11, 22, 33], [0, 51, 255], [0, 0, 0]], u'start': 0, u'sel': True, u'pal': 8, u'id': 0}], u'bri': 255, u'pl': -1}

I have had no problem extracting the states I need with statusjson["transition"] for example. I am struggling to then get the elements I need from the "seg" portion as when I try to get just that with
Code: Select all
segjson = statusjson.get("seg")

I get the following
Code: Select all
[{u'ix': 128, u'cln': -1, u'sx': 170, u'fx': 54, u'rev': False, u'stop': 151, u'len': 151, u'col': [[11, 22, 33], [0, 51, 255], [0, 0, 0]], u'start': 0, u'sel': True, u'pal': 8, u'id': 0}]

Which I can no longer extract individual elements, and I am out of my depth. I am especially interested in the "col" values, but also the "pal" but I don't seem to be able to reference those elements at all.

Any pointers ?

Posted on
Sat Nov 09, 2019 3:46 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Struggling with decoding this JSON

What you're dealing with are dicts and lists. You don't need to use the 'get' part--usually that's only used if you don't know if a key exists. For example,
Code: Select all
x = dict.get('foo', 123)

Dict elements are located by using their keys. List elements are found by using their indexes (as they are with tuples).
Code: Select all
x = {'a': 1, 'b': 2, 'c': 3}
y = [1, 2, 3]

print(x['a'])

print(y[0])

Here are some examples that will hopefully set you on the course:
Code: Select all
print(j['info'])

print(j['info']['opt'])

print(j['state'])

print(j['state']['seg'])

for thing in j['state']['seg']:
    print(thing)
    print(thing['fx'])
   
print(j['state']['seg'][0]['fx'])

Dicts and lists are hugely useful. There are tons of tutorials online that explain how to use them.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sat Nov 09, 2019 3:56 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Struggling with decoding this JSON

Not positive what you're needing, but statusjson is a dictionary. 'state' is a dictionary inside the statusjson dictionary. To get the 'on' value of the 'state' dictionary, you'd get it like this:

Code: Select all
statusjson['state']['on']


That would return the boolean True.

Likewise, if you wanted to get the 'pal' value in the 'seg' dictionary in the 'state' dictionary:

Code: Select all
statusjson['state']['seg']['pal']


that would return the numeric value 8.

The 'col' value inside 'seg' inside 'state' is a list of 3 lists. So:

Code: Select all
col_list = statusjson['state']['seg']['col']


returns the following list:

Code: Select all
[[11, 22, 33], [0, 51, 255], [0, 0, 0]]


To get the first list ([11, 22, 33]):

Code: Select all
first_col_list = col_list[0]


(python lists are 0 indexed, so the first item is 0, second item is 1, etc).

To the 33:

Code: Select all
third_value_of_first_col_list = first_col_list[2]


[EDIT] looks like Dave and I were answering at the same time. Basically, you should read up on Python Dictionaries and Lists, which will make this much clearer.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Nov 09, 2019 4:36 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Struggling with decoding this JSON

jay (support) wrote:
[EDIT] looks like Dave and I were answering at the same time. Basically, you should read up on Python Dictionaries and Lists, which will make this much clearer.


Jinx. Buy me a Coke.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sat Nov 09, 2019 4:41 pm
neilk offline
Posts: 715
Joined: Jul 13, 2015
Location: Reading, UK

Re: Struggling with decoding this JSON

Thanks both, much appreciated and I will try and build my base knowledge up. I tend to learn by trying, but realise no substitute for a bit of structure in the foundation. Having something to try really helps, and I had hit a bit of a brick wall.

Posted on
Sat Nov 09, 2019 5:43 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Struggling with decoding this JSON

No worries whatsoever--we were all there once. Nobody gets there alone.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sat Nov 09, 2019 5:52 pm
neilk offline
Posts: 715
Joined: Jul 13, 2015
Location: Reading, UK

Re: Struggling with decoding this JSON

So something a little strange is going on. I think I have my head around most of this now, and have this working, apart for things in the "seg", which appears to be a list with one value.
when I try
Code: Select all
statusjson['state']['seg']['pal']

I get the following error (all of the other references are fine and worked as I thought they should)
Code: Select all
TypeError: list indices must be integers, not str

and I get the same if I try
Code: Select all
col_list = statusjson['state']['seg']['col']

And it looks to me like "seg" is a list with a single item containing
Code: Select all
{u'ix': 128, u'cln': -1, u'sx': 170, u'fx': 54, u'rev': False, u'stop': 151, u'len': 151, u'col': [[11, 22, 33], [0, 51, 255], [0, 0, 0]], u'start': 0, u'sel': True, u'pal': 8, u'id': 0}

As this is what is returned when I try the following
Code: Select all
col_list = statusjson['state']['seg'][0]


I will keep working from the bottom up to see as this is almost certainly me missing something !

Posted on
Sat Nov 09, 2019 6:17 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Struggling with decoding this JSON

You're getting there. The error is a hint that you're hitting on a list with a dictionary key.

Try this:
Code: Select all
col_list = statusjson['state']['seg'][0]['col']

It looks like that reference will return a list of lists. :D

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Nov 10, 2019 3:46 am
neilk offline
Posts: 715
Joined: Jul 13, 2015
Location: Reading, UK

Re: Struggling with decoding this JSON

That was it, the levels of nesting was making my head explode. Thanks so much for taking the time, I thought I was getting in the abstract and with simply examples but when the rubber hit the road!

This community is truly enriched by what you do, and I appreciate it, and also for the dedication of Jay and Matt.

Neil

Posted on
Sun Nov 10, 2019 7:07 am
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Struggling with decoding this JSON

No worries. Once you come to grips with the way that dictionaries, lists and tuples work, you'll find that they're used all over the place (because they are super useful). Be sure to check out some tutorials on these types of data structures because there are a few tricks and traps along the way that you might not expect. Take 'slicing' for instance--you can take a slice of a list (or a tuple) with this structure:

Code: Select all
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

print(x[0:4])

which yields '1, 2, 3, 4'. The slice goes through to index 3--the second slice index goes "through but not including"--so 0 to 4 gives you the 0, 1, 2, 3 indexes. Like I said, maybe not what you'd expect, but it makes sense once you know about it. There are other funny bits too. For example:
Code: Select all
print(x[-1])

Happy to help. I got a lot of lifts from folks here as I was getting started.

Cheers.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Nov 10, 2019 4:47 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Struggling with decoding this JSON

Yep - dictionaries and lists are critical parts of Python - you can't do much without understanding those. I highly recommend completing an intro Python tutorial - it won't take long (a couple of hours probably) and they usually have good examples as you go. Building a solid baseline understanding will keep you out of trouble in the future and will make it much easier to dive into the Python docs when you need something further.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Nov 11, 2019 10:46 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Struggling with decoding this JSON

jay (support) wrote:
and will make it much easier to dive into the Python docs when you need something further.

I’m glad I’m not the only one that still has to!


Sent from my iPhone using Tapatalk Pro

Posted on
Mon Nov 11, 2019 3:08 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Struggling with decoding this JSON

howartp wrote:
jay (support) wrote:
and will make it much easier to dive into the Python docs when you need something further.

I’m glad I’m not the only one that still has to!


I don't think anyone (except possibly the actual contributors to Python) know everything off the top of their heads. Reference docs are there for a reason! :lol:

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Jan 05, 2020 8:53 am
neilk offline
Posts: 715
Joined: Jul 13, 2015
Location: Reading, UK

Re: Struggling with decoding this JSON

Just a little follow up - I did as Jay suggested and completed the Microsoft online course which took a few hours but was well worth the effort. I am a good way into plugin number 2, which may end up being for an audience of me but I still wanted to do it. I have been able to be way more effective with the base learning and support without having to call for help (so far). Still some schoolboy errors from me, but I am much better at figuring out why it isn’t working. I did get in a hole around plugin props vs prefs but figured it out by picking through the docs and other plugins.

It is quite rewarding figuring this stuff out and when faced with a use case being able to design a workable plugin. My second one will be much cleaner and more elegant thanks to the help and pointers.

Neil

Posted on
Sun Jan 05, 2020 10:34 am
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Struggling with decoding this JSON

I think a lot of plugins started out as solutions to the authors' problems and grew from there.

Personally, I'm still consistently learning new and better ways of doing things. For me, that's half the fun.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Who is online

Users browsing this forum: No registered users and 7 guests