Json.Append question

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
busterbluth
Cave Troll
Posts: 30
Joined: Sat Aug 17, 2019 3:49 pm

Json.Append question

Post by busterbluth »

Hi, i've got a macro where JSON.Append is adding an empty '[]' to a JSON array. Any ideas why?

Code: Select all

[h: ID = arg(0)]
[h: sAct = arg(1)]

	<!--update PhaseInfo to show dice action used in current phase-->
	[h: newPhaseInfo = json.set("","ActionTaken",sAct)]
	[h: PropPI = getProperty("PhaseInfo",ID)]
	[h: PropPI = json.append(newPhaseInfo, PropPI)]



[h: macro.return = PropPI]

The result looks like this:

Code: Select all

[{"ActionTaken":"Martial Artist"},[]]
I'm taking the returned result of the function above and storing it in a token property. Thanks in advance!

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Json.Append question

Post by aliasmask »

It's empty because PropPI is an empty array.

busterbluth
Cave Troll
Posts: 30
Joined: Sat Aug 17, 2019 3:49 pm

Re: Json.Append question

Post by busterbluth »

OK, so how should I create a new JSON object to an initially empty property and then add it to the array? Isn't this how the wiki says to do it?

Trying to read a JSON array when the last element = '[]' is making my code error out. Oddly enough, I've got a different macro taking the same approach to adding the first element to a JSON array and I don't get the '[]' as the last element.

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Json.Append question

Post by aliasmask »

Can't really say what is right and wrong with your data without context, but if you want the 2nd element of an array you use json.get(array,1). Then that element is an array and if there is data in there you would use another json.get(array2,0).. But if the array is empty, then you get an error. You need to test to see if the array is empty first, if(json.isEmpty(array)): do whatever...

busterbluth
Cave Troll
Posts: 30
Joined: Sat Aug 17, 2019 3:49 pm

Re: Json.Append question

Post by busterbluth »

I know how to read a JSON array; the problem I'm having is that JSON.Append isn't appending correctly, whether the JSON array is empty or not. I changed my token property have a default value of :

Code: Select all


[{"ActionTaken":"default"}]

and this is what the same property looks like after the macro runs a couple of JSON.appends:

Code: Select all

[{"ActionTaken":"Blocking"},[{"ActionTaken":"Martial Artist"},[{"ActionTaken":"default"}]]]
Each new object added is getting the surrounding '[]' characters, as opposed to simply enclosing the whole array with a single pair of them. I'm using an older version, 1.10.4, but it's not that old. Any idea what's going on here? In the same campaign file, I have another simple macro using json.append in an identical manner which works perfectly:

Code: Select all



[h: gadget = json.set("", "Gadget",sName, "Desc",sDesc,
"AV",iAV,"EV",iEV,"Rel",iRN,"Ammo",iAmmo,"Body",iBody,"Linked",sLink, "Melee",iMelee)]

[h: Gadgets = json.append(gadget, Gadgets)]

Here's an example Gadgets property value copied from one of the tokens in the file after getting updated:

Code: Select all

[{"Gadget":"White Sound Gen.","Desc":"Sonic Beam","AV":0,"EV":10,"Rel":4,"Ammo":0,"Body":6,"Linked":"DEX","Melee":0},{"Gadget":"Grappling Hook + Winch","Desc":"5 APs long","AV":0,"EV":9,"Rel":0,"Ammo":0,"Body":6,"Linked":"DEX","Melee":0},{"Gadget":"Laser Attachment","Desc":"Heat Vision","AV":0,"EV":7,"Rel":3,"Ammo":0,"Body":6,"Linked":"DEX","Melee":0}]
This is absolutely perfect! The only difference i can see between the two pieces of code is that the Phase Info code is a called function whereas the Gadget code is not. Any idea what's going on here?

busterbluth
Cave Troll
Posts: 30
Joined: Sat Aug 17, 2019 3:49 pm

Re: Json.Append question

Post by busterbluth »

Looks like I figured this out. Switching the arguments in JSON.append is working:

Code: Select all


	[h: PropPI = json.append( PropPI,newPhaseInfo)]

which gives me a property that looks like this:

Code: Select all

[{"ActionTaken":"Martial Artist"},{"ActionTaken":"Blocking"}]
which means I can now use other JSON functions to reference the property. Thanks again for the assist!!

Post Reply

Return to “Macros”