json.append not working like I thought?

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: dorpond, trevor, Azhrei

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
User avatar
celestian
Dragon
Posts: 276
Joined: Mon May 17, 2010 3:29 pm

json.append not working like I thought?

Post by celestian »

I'm trying to append to a json but can't seem to come up with a good way to do it. What I'd like to have is a list of token IDs (sometimes the same ones) with details on state/duration/etc. That way each round I can flip through the list and tic down the time on the state duration and eventually remove the state icon. Problem is I can't figure out the "-->" part listed below. ([h: statesJSON = json.append(statesJSON,addThisJSON)])

Code: Select all

[h: assert(getSelected()!='',"You must select token(s) to use this command.",0)]

[h: campaignStatesJSON = json.get(getInfo("campaign"),"states")]
[h: statesTokenJSON = json.get(campaignStatesJSON,"Tokens")]
[h: statesList = json.toList(statesTokenJSON)]

[h: stateSelectInput = strformat("stateID | %{statesList} | Select State Type | LIST | SELECT=0 VALUE=STRING")]
[h: durationInput = strformat("duration| 1 | State duration in rounds")]
[h:	conversionSet1 = strformat("junkvar| 10 rounds | Turn | LABEL")]
[h:	conversionSet2 = strformat("junkvar| 60 rounds | Hour | LABEL")]
[h:	conversionSet3 = strformat("junkvar| 1440 rounds | Day | LABEL")]

[H: status = input(
		stateSelectInput,
		conversionSet1,
		conversionSet2,
		conversionSet3,
		durationInput
)]
[H: abort(status)]

[h: statesJSON = getLibProperty("tokenStates","Lib:ADND")]

[h: currentSelections = getSelected()]
[foreach(id,currentSelections,""), code :{
	[h: addThisJSON = "{}"]
	[h: addThisJSON = json.set(addThisJSON,"tokenID",id)]
	[h: addThisJSON = json.set(addThisJSON,"stateID",stateID)]
	[h: addThisJSON = json.set(addThisJSON,"duration",duration)]
[h:'-->'][h: statesJSON = json.append(statesJSON,addThisJSON)]
	[h: setState(stateID,id,1)]
}]

[h: setLibProperty("tokensStates",statesJSON,"Lib:ADND")]
Value of addThisJSON is :

Code: Select all

{"tokenID":"000000002E13816D8560000000000000","stateID":"Acid","duration":1} 
I COULD use

Code: Select all

statesJSON = json.set(tokenID,addThisJSON)
but that would over write a previously set state for the same tokenID. I want to avoid having to do checks to see if the ID exists and just append the IDs with the state and flip through those in another "time" macro.

Anyone have a suggestion on how to do this?

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: json.append not working like I thought?

Post by wolph42 »

what you want is an json array of json objects (i think) now you are trying to create a jsonobjectarray (which does not exist) So you structure would look like:
[{tokenid:..., etc}, {tokenid:etc}, etc]

this means that your states json needs to be an array e.g.: [1,2,3,4] and not an object eg: {a:1, b:2}. When its an array you can append the objects to it.

I think its enough to simply change the lib property to '[]'. Try that and run your code again.

User avatar
celestian
Dragon
Posts: 276
Joined: Mon May 17, 2010 3:29 pm

Re: json.append not working like I thought?

Post by celestian »

wolph42 wrote:what you want is an json array of json objects (i think) now you are trying to create a jsonobjectarray (which does not exist) So you structure would look like:
[{tokenid:..., etc}, {tokenid:etc}, etc]

this means that your states json needs to be an array e.g.: [1,2,3,4] and not an object eg: {a:1, b:2}. When its an array you can append the objects to it.

I think its enough to simply change the lib property to '[]'. Try that and run your code again.
That did it, though I had a typo in the properties as well which fixing helped also.

Thanks! Works like a charm now.

For the benefit of everyone that might have similar issues here is the macro I ended up with.

Code: Select all

[h: assert(getSelected()!='',"You must select token(s) to use this command.",0)]

[h: campaignStatesJSON = json.get(getInfo("campaign"),"states")]
[h: statesTokenJSON = json.get(campaignStatesJSON,"Tokens")]
[h: statesList = json.toList(statesTokenJSON)]

[h: stateSelectInput = strformat("stateID | %{statesList} | Select State icon | LIST | SELECT=0 VALUE=STRING")]
[h:	conversionSet1 = strformat("junkvar| 10 rounds | Turn | LABEL")]
[h:	conversionSet2 = strformat("junkvar| 60 rounds | Hour | LABEL")]
[h:	conversionSet3 = strformat("junkvar| 1440 rounds | Day | LABEL")]
[h: durationInput = strformat("duration| 1 | State duration in rounds")]
[h: nameInput = strformat("name| Sleep | Name of effect.")]
[h: durationInputTurn = strformat("durationTurn| 0 | Turn count")]
[h: durationInputHour = strformat("durationHour| 0 | Hour count")]
[h: durationInputDay = strformat("durationDay| 0 | Day count")]

[h: time_day = 1440]
[h: time_hour = 60]
[h: time_turn = 10]

[H: status = input(
		stateSelectInput,
		nameInput,
		durationInput,
		durationInputTurn,
		durationInputHour,
		durationInputDay
)]
[H: abort(status)]

[h: assert(isNumber(durationDay),"The day input value was not a number.",0)]
[h: assert(isNumber(durationHour),"The hour input value was not a number.",0)]
[h: assert(isNumber(durationTurn),"The turn input value was not a number.",0)]
[h: assert(isNumber(duration),"The duration input value was not a number.",0)]

[h: duration = (durationDay*time_day)+(durationHour*time_hour)+(durationTurn*time_turn)+duration]

[h: statesJSON = getLibProperty("tokensStates","Lib:ADND")]

[h: currentSelections = getSelected()]
[foreach(id,currentSelections,""), code :{
	[h: addThisJSON = "{}"]
	[h: tokenName = getName(id)]
	[h: addThisJSON = json.set(addThisJSON,"tokenID",id)]
	[h: addThisJSON = json.set(addThisJSON,"name",name)]
	[h: addThisJSON = json.set(addThisJSON,"stateID",stateID)]
	[h: addThisJSON = json.set(addThisJSON,"duration",duration)]
	[h: statesJSON = json.append(statesJSON,addThisJSON)]
	[h: setState(stateID,1,id)]
	[h: showIt(strformat('%{name} (icon:%{stateID}) effect added to %{tokenName} for %{duration} round(s).<br>'),id,"attack",0)]
}]

[h: setLibProperty("tokensStates",statesJSON,"Lib:ADND")]


Post Reply

Return to “MapTool”