Testing if json.get returned something

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
Mrugnak
Dragon
Posts: 745
Joined: Mon Jul 21, 2008 7:38 pm

Testing if json.get returned something

Post by Mrugnak »

My intent was to set up a default json object if one is not provided.

Code: Select all

[ThisWeapon=json.get('{"Flintlock": {"MinnyBall": {
    "DamageBase1": "4d6+2",
    "ArmorDivisor1": "",
    "DamageType1": "pi++",
    "DamageExplosion1": 0,
    "DamageFrag1": 0,
    "ROF1": 1,
    "Notes": ""
}}}', "Invalid")]<br>[ThisWeapon = if(ThisWeapon=="", "{}" , ThisWeapon)]
Here, we're looking for an invalid key, and thus the IF works as expected, and sets ThisWeapon to be an empty JSON instead of an empty string.

BUT. Should I look up a valid key, as in the following snippet, I get the fabulous "Error in body of roll" message,

Code: Select all

[ThisWeapon=json.get('{"Flintlock": {"MinnyBall": {
    "DamageBase1": "4d6+2",
    "ArmorDivisor1": "",
    "DamageType1": "pi++",
    "DamageExplosion1": 0,
    "DamageFrag1": 0,
    "ROF1": 1,
    "Notes": ""
}}}', "Flintlock")]<br>[ThisWeapon = if(ThisWeapon=="", "{}" , ThisWeapon)]
I've confirmed the json.get is working. The value of the "Flintlock" key can be anything, a simple string, a JSON list, or a JSON object, the following conditional still barfs.

Worse, if I try using a roll option instead, it still barfs -- but again only if the json.get works.

Code: Select all

[ThisWeapon=json.get('{"Flintlock": {"MinnyBall": {
    "DamageBase1": "4d6+2",
    "ArmorDivisor1": "",
    "DamageType1": "pi++",
    "DamageExplosion1": 0,
    "DamageFrag1": 0,
    "ROF1": 1,
    "Notes": ""
}}}', "Flintlock")]<br>[if(ThisWeapon==""): ThisWeapon = "{}")]
I have since found that [if(json.isEmpty(ThisWeapon)): ThisWeapon = "{}")] works, which helps me right now - BUT: What's going on that I can't check the result?

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Testing if json.get returned something

Post by aliasmask »

You should use the constructors to build your json. Also, although json looks like a string and can be treated as one, it's not. So, the compare to "" will give an error if the other side is not a string. I use json.isEmpty() because returns true is null, empty string, empty array or empty object and doesn't throw an error.

User avatar
Mrugnak
Dragon
Posts: 745
Joined: Mon Jul 21, 2008 7:38 pm

Re: Testing if json.get returned something

Post by Mrugnak »

aliasmask wrote:You should use the constructors to build your json.
Thank you, I did. It was built in an interactive macro and spamming the forum with it would distract from the thing, so I dumped the result (it's actually stored as a property on the token). I did LINT the JSON to make sure I wasn't wasting anyone's time by improperly transcribing it to the forum.
aliasmask wrote:Also, although json looks like a string and can be treated as one, it's not. So, the compare to "" will give an error if the other side is not a string. I use json.isEmpty() because returns true is null, empty string, empty array or empty object and doesn't throw an error.
Useful to know, I'm not used to languages outside Perl returning one-or-many different data types from the same function call. Is there a way to test the object type? I see ways to force interpretation as a string or number, but no obvious way to test if it's a string, a number, or a JSON.

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Testing if json.get returned something

Post by aliasmask »

I recommend familiarizing yourself with the functions list in the wiki just to get an idea of what's available. The names are usually descriptive enough barring some minor details. There's also a couple of category groups for related functions.

What you're looking for is Wiki: json.type().

User avatar
Mrugnak
Dragon
Posts: 745
Joined: Mon Jul 21, 2008 7:38 pm

Re: Testing if json.get returned something

Post by Mrugnak »

aliasmask wrote:I recommend familiarizing yourself with the functions list in the wiki just to get an idea of what's available.
Thanks, but I've been reading it for years.
aliasmask wrote:What you're looking for is Wiki: json.type().
No, it's not - although it's a useful part of the solution. It doesn't tell me "is string" or "is number", which is just as important.

User avatar
metatheurgist
Dragon
Posts: 364
Joined: Mon Oct 26, 2009 5:51 am

Re: Testing if json.get returned something

Post by metatheurgist »

Mrugnak wrote:Thanks, but I've been reading it for years.
Then you already know about isNumber(). I think if it's not a number it's a string in MT.

Post Reply

Return to “Macros”