How to sort data from API?

Hi there,

I've got a API that I want to collect and sort data from. When i use HTTP binding i get this result. I assume this is JSON, but I don't know what I should do next to sort it. Any advice for how to use this is very appreciated :slight_smile:

image

When i bind the HTTP request to a custom, it looks like this. And i get invalid key?

Well, the first question is "what do you want to sort it by"?

Currently you're getting an object - a deliberately unsorted mapping of string keys to arbitrary values - in this case the keys look like UUIDs.

So at a minimum, if you want a sorted output, you'll have to change to the only JSON structure that supports ordering, an array. There's already an id key, which is nice; you could (for a start) just use a transform script that loops over the object keys in encounter order and outputs a Python list, which will be returned as a JSON array.

Thanks. This is what it looks like in node red, and also how I want it to display in Ignition, in order to use the data.

I mean, that's what you have in Ignition.
It's just that Node-RED is displaying the object keys in encounter order, while in the Perspective property editor we're displaying them in some arbitrary order in the editor. Neither behavior is correct or incorrect per the spec; just different.

You're getting invalid key errors because it's bad programming practice to use 'kekab-case' for identifiers that might show up in programming contexts, because it's hard to distinguish between someObject.someKey-WithAHyphen and someObject.someKey-subtractedBySomethingElse.

You can try just adding a script transform:

return list(value.values())
4 Likes

That looks nice! thank you :slight_smile:

1 Like

Only thing, its a little hard to find the data I want... Is it possible to make the listing sorted by name?

image

:smile:

return sorted(value.values(), key = lambda o: o.name)

Should work.

You'r right :laughing:

Still looks the same :confused:

image

No, you cannot put the name where the array index is. Do open some objects to see the sort order.

2 Likes

Sure you can, convert the array into an object and use the name as the keys. It will become unsorted again though...

return {row['name']: row for row in values}