is there a way to sort json objects?

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
User avatar
wolph42
Deity
Posts: 9805
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

is there a way to sort json objects?

Post by wolph42 »

a quick question:
is there a(n easy) way to sort json objects?
I know that json.sort does arrays, but is there also something for objects?

User avatar
plothos
Great Wyrm
Posts: 1890
Joined: Sat Jun 21, 2008 1:07 am

Re: is there a way to sort json objects?

Post by plothos »

Nope, I don't think so.
You could maybe make a udf, though.
use json.fields() to get a list, sort the list and create a new object by calling the original values in that order. I'm assuming the order of the object is reflective of the order the fields are added, though this may not be true.
Drop-In Utilities:
My Spell Manager for D&D3.5 and PFRPG
My Inventory Manager for D&D and PFRPG, but more generally useable than that.
My Message Manager -- My Top-Down D&D Token Images
and my Custom Initiative & Status/Spell-Effect Tracker (work in progress, but functional).

User avatar
biodude
Dragon
Posts: 444
Joined: Sun Jun 15, 2008 2:40 pm
Location: Montréal, QC

Re: is there a way to sort json objects?

Post by biodude »

There's an example on the wiki of a UDF for [wiki]Sorting Nested JSON Objects[/wiki] (by sub-fields), if that's what you're looking for.
Look under [wiki]JSON Sorting[/wiki] for the code.
"The trouble with communicating is believing you have achieved it"
[ d20 StatBlock Importer ] [ Batch Edit Macros ] [ Canned Speech UI ] [ Lib: Math ]

User avatar
aliasmask
Deity
Posts: 8624
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: is there a way to sort json objects?

Post by aliasmask »

I'm not sure and you may want to test, but I think the order remains the same once you set a keyword. So, if your object doesn't change too much, adding and removing keys, then you could use json.fields and return it as a json array. Then use json.sort on the array. Use a foreach to write a new obj in your needed order and replace the old obj with the new obj. Then it will remain sorted and you won't have to keep using json.fields and json.sort. You can put the obj in to foreach and it should be in the correct order.

User avatar
wolph42
Deity
Posts: 9805
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: is there a way to sort json objects?

Post by wolph42 »

thnx for the suggestions. I feared that there was no easy way, but creating a udf shouldn't be too hard. Its only the fields I wanted sorted not the nested objects.

prestidigitator
Dragon
Posts: 317
Joined: Fri Apr 23, 2010 8:17 pm

Re: is there a way to sort json objects?

Post by prestidigitator »

I don't believe there are any guarantees, so even if you manage to find some hack to get it to work now, there may be a point where it stops working in the future. I'd look at WHY you want to sort them, and that might give you a clue as to where and how to do it. You may be able to do it at the point where you are retrieving and using the values from the object, or you may want to add some kind of special field that holds (a cached array of) the sorted names of the other fields.
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton

User avatar
wolph42
Deity
Posts: 9805
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: is there a way to sort json objects?

Post by wolph42 »

i need it because I regularly output the fields to screen, currently i just solved it by listSort(json.fields(etc)) however as changes are much less frequent than screenupdates its faster if they stored sorted, hence my question. Though a sotred in chache is also an interesting idea.

Post Reply

Return to “Macros”