Suddenly having errors with json.get

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
ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Suddenly having errors with json.get

Post by ziltmilt »

I'm getting this error when running some code:

Code: Select all

    Invalid condition in IF(json.get(item, "Entity") == sName) roll option.       Statement options (if any): h, if(json.get(item, "Entity") == sName )       Statement Body : totalWeight = totalWeight + (json.get(item, "Qty") * json.get(item, "Weight"))
What's strange is that everything was running fine prior to last night; as far as I can recall, I haven't made any changes to my code in several days. I've got this function in a lib token, GetCarriedWeight:

Code: Select all

[h: sName = arg(0)]
[h: totalWeight = 0]
[h: Items = "[]"]
[h: Inventory = getLibProperty("Inventory","Lib:Utility")]
[h: Containers = getLibProperty("Containers","Lib:Utility")]
[h, foreach(item, Inventory), CODE: {

	[h, if(json.get(item, "Entity") == sName ): totalWeight = totalWeight + 
	(json.get(item, "Qty") * json.get(item, "Weight"))]
	
	[h, if(json.get(item, "Capacity") > 0): Containers = json.append(Containers, item)]

	[h, if(json.get(item, "Entity") == sName ): Items = json.append(Items, item)]	

}]
[h: setLibProperty("ItemList",Items, "Lib:Utility")]
[h: setLibProperty("Containers",Containers, "Lib:Utility")]
[h: macro.return = totalWeight ]
sName is the name of each selected token. If i take out the code in the foreach loop, this executes with no errors. But it doesn't like any of the lines in the foreach, either individually or as you see above. The Inventory library property is populated with a string, which is a JSON array, which looks like this:

Code: Select all

,{"ID":2840,"ItemName":"Periapt of Health","ItemDesc":"pg 325","Container":1775,"Entity":"Perran Davy","Qty":1,"Weight":0.01,"Capacity":0,"WeightOverride":0,"MagicItem":1},{"ID":2845,"ItemName":"Spear +1","ItemDesc":"head of adamantine steel","Container":0,"Entity":"Zaor","Qty":2,"Weight":5,"Capacity":0,"WeightOverride":0,"MagicItem":0},{"ID":2848,"ItemName":"Decanter of endless water","ItemDesc":"","Container":1839,"Entity":"Cotman Longfoot","Qty":1,"Weight":2,"Capacity":0,"WeightOverride":0,"MagicItem":1},{"ID":2861,"ItemName":"Water, quart","ItemDesc":"","Container":2582,"Entity":"Perran Davy","Qty":1,"Weight":2,"Capacity":0,"WeightOverride":0,"MagicItem":0},{"ID":2865,"ItemName":"Lantern, hooded","ItemDesc":"","Container":0,"Entity":"Cotman Longfoot","Qty":1,"Weight":2,"Capacity":0,"WeightOverride":0,"MagicItem":0},{"ID":2866,"ItemName":"Holy water (flask)","ItemDesc":"","Container":1839,"Entity":"Cotman Longfoot","Qty":4,"Weight":0.1,"Capacity":0,"WeightOverride":0,"MagicItem":0}
If it helps any, I can post the code for the calling function. Any pointers on what I've wrong?

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

Re: Suddenly having errors with json.get

Post by aliasmask »

You have to be careful when using the == comparison operator. Both sides have to be of the same type, like string == string. You can't mix like number == string or json == string. Are you using the latest MT version?

This could be a pasting error, but the json you're showing isn't a valid json with the , in front. This could also cause that error.

ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Re: Suddenly having errors with json.get

Post by ziltmilt »

Right about the sample JSON .. it's just a portion. This is the first part of it:

Code: Select all

[{"ID":1735,"ItemName":"Shield +1","ItemDesc":"","Container":0,"Entity":"Zaor","Qty":1,"Weight":4,"Capacity":0,"WeightOverride":0,"MagicItem":0},{"ID":1738,"ItemName":"Dagger +2, +3 vs goblin, orc, kobolds","ItemDesc":"","Container":0,"Entity":"Zaor","Qty":1,"Weight":1,"Capacity":0,"WeightOverride":0,"MagicItem":1},{"ID":1745,"ItemName":"Backpack","ItemDesc":"","Container":0,"Entity":"Zaor","Qty":1,"Weight":0.1,"Capacity":40,"WeightOverride":0,"MagicItem":0},{"ID":1775,"ItemName":"Backpack","ItemDesc":"","Container":0,"Entity":"Perran Davy","Qty":1,"Weight":0.1,"Capacity":40,"WeightOverride":0,"MagicItem":0},{"ID":1799,"ItemName":"Quarterstaff ","ItemDesc":"Turkey Sandwhich","Container":0,"Entity":"Perran Davy","Qty":1,"Weight":4.5,"Capacity":0,"WeightOverride":0,"MagicItem":0},{"ID":1809,"ItemName":"Shield +1","ItemDesc":"Adamantine round spiked shield","Container":0,"Entity":"Lonmel Darkmail","Qty":1,"Weight":5,"Capacity":0,"WeightOverride":0,"MagicItem":0},{"ID":1815,"ItemName":"Sling ","ItemDesc":"","Cont ...
Last night, i was using 1.6.1, but upgraded this morning to 1.7. Got the same error on both versions.

sName = the token name. So, I thought i was comparing strings on both sides of the == operator. Here's the calling code:

Code: Select all

[h,foreach(Selected, getSelected("json")), CODE:
{

	[h: setLibProperty("Containers","[]", "Lib:Utility")]
	[h: sTokenRace = getProperty("Race", Selected)]
	[h: iVal = getCarriedWeight(getName(Selected))] 
	...
	
Could getName be returning a different datatype than a string?

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

Re: Suddenly having errors with json.get

Post by aliasmask »

for debugging I usually have a function I call to validate the data I'm using. I have a function called watch() where I pass the string names of the variables I want to look at. So, in your example, before the if, I would put

Code: Select all

[H: entityName = json.get(item, "Entity")]
[H: watch("entityName","sName")]
That will post a dialog on each iteration of the loop with those values and if there is an error I can see what names it didn't like. You can using my signature to get the token I use for debugging stuff like this. It's on the lib:token as an example default token I use for all my projects.

ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Re: Suddenly having errors with json.get

Post by ziltmilt »

When I have only this line in the CODE block:

Code: Select all

[H: entityName = json.get(item, "Entity")]
I get this error :

Code: Select all

Argument number 2 "Entity" to function "json.get" must be an integer.

This really stumps me. I've always used a string as the 2nd argument in json.get. It's like Maptool has gone all Crazy-Town on me.

ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Re: Suddenly having errors with json.get

Post by ziltmilt »

Figured it out !!!


I had a quotation mark embedded in one of the string properties in the JSON Inventory variable value. That threw off Maptool's ability to interpret the string.

Many thanks for the help, guys.

BTW, we had another great session this past Saturday. Had a buddy join us who had never used a VTT before and his initial reaction was 'Where was this thing 25 years ago???" So, he was very impressed by Maptool's capabilities.

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

Re: Suddenly having errors with json.get

Post by busterbluth »

Getting another weird result with JSON.Get. I've got this JSON object array in a library token named Utility; the property is called Skills and the 1st object in the array looks like this:

Code: Select all

[{"PSA":"Military","Skill":"Beam Weapons","Subskill":"none","Attribute":"Dexterity","BaseSuccess":0.5,"Opposed":"N"},{ ...
I won't post the entire property value; it's over 6300 characters long.

I"m trying to write a generic function that will create a list from unique values in specific fields found in the object array:

Code: Select all


[h: sArrName = arg(0)]
[h: sField = arg(1)]
[h: arrObjs = getLibProperty(sArrName,"Lib:Utility")]
[h: objList = ""]
[h, foreach(obj, arrObjs), CODE: {
	[h: setLibProperty("Debug",obj,"Lib:Utility")]
	[h: objList = json.append(objList, json.get(obj, sField))]
}]
[h: objList = json.unique(objList)]
[h: sList = json.toList(objList)]
[h: macro.return = sList]

What's odd here is that the foreach roll option isn't picking up the entire object. When this function is called, it gives me the same error at the start of the threat (JSON's 2nd parameter should be an integer). But, it's parsing only this as the object value:

Code: Select all

Debug Value = [ {"PSA":"Military"
So, not only is foreach only retrieving part of the object, it's also bringing in the '[' opening bracket character. I'm stumped as to why this isn't working. Any ideas out there?

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

Re: Suddenly having errors with json.get

Post by busterbluth »

False alarm .. this was a data problem (not sure where, but I just re-created the JSON object array to get a clean version).

The macro logic works fine.

Post Reply

Return to “Macros”