req: JSON for Maptool tutorial

Doc requests, organization, and submissions

Moderators: dorpond, trevor, Azhrei

Post Reply
User avatar
DrVesuvius
Giant
Posts: 199
Joined: Wed Jun 18, 2008 2:07 pm

req: JSON for Maptool tutorial

Post by DrVesuvius »

Could anyone give us a bit of a Dummies Guide intro to the use of JSON objects in MapTool? Something similar to the excellent walkthru that we got for StrProps and Lists in the b42 revision would be great.

I kinda get the idea of it being a flexible recursive data structure, but exactly how to translate that into practical usage, I just don't quite get it yet. And since it looks like JSON is the way forward with MapTool....

Dr V

User avatar
palmer
Great Wyrm
Posts: 1367
Joined: Sat Sep 06, 2008 7:54 pm

Re: req: JSON for Maptool tutorial

Post by palmer »

Agreed. I know it exists, but I have absolutely no idea how to use it.

Mind you, taking several of k.fan's examle macros (like the skill check one) and rewriting them to use JSON would probably also help in understanding how to actually use it.

bobthedog
Cave Troll
Posts: 85
Joined: Sun Aug 03, 2008 7:17 pm

Re: req: JSON for Maptool tutorial

Post by bobthedog »

As for "when/why" to use them, if you're using StrProps and StrLists, JSONs work similarly. The "how" is what's different.

There is a big thread here discussing the main differences.

The only thing that really bugs me with JSONs (and if someone could show me the way to do it differently, thanks) is that there is no "varsFromJSON()" function, and you have to "manually" assign vars, such as:

Code: Select all

[Weapon = json.set("{}", "Name": "Dagger", "Damage": 4, "Speed": 1)]
[Name = json.get(Weapon, "Name")]
[Damage = json.get(Weapon, "Damage")]
[Speed = json.get(Weapon, "Speed")]
Which means that big JSONs require LONG lists of variable assignment...


User avatar
Rumble
Deity
Posts: 6235
Joined: Tue Jul 01, 2008 7:48 pm

Re: req: JSON for Maptool tutorial

Post by Rumble »

bobthedog wrote:As for "when/why" to use them, if you're using StrProps and StrLists, JSONs work similarly. The "how" is what's different.

There is a big thread here discussing the main differences.

The only thing that really bugs me with JSONs (and if someone could show me the way to do it differently, thanks) is that there is no "varsFromJSON()" function, and you have to "manually" assign vars, such as:

Code: Select all

[Weapon = json.set("{}", "Name": "Dagger", "Damage": 4, "Speed": 1)]
[Name = json.get(Weapon, "Name")]
[Damage = json.get(Weapon, "Damage")]
[Speed = json.get(Weapon, "Speed")]
Which means that big JSONs require LONG lists of variable assignment...
Here's something I just thought of. Call it a varsFromJSON hack:

Code: Select all

<!-- Create Test JSON -->
[h:testObj = '{"alpha":"1d10", "bravo":6, "charlie":"foo", "delta":"boogers", "echo":99}']

<!-- Generate list of fields in the JSON object -->
[h:varList=json.fields(testObj)]

<!-- loop through that list -->
[h,foreach(var,varList),CODE:
{
     <!--get the value at each key in the list-->
     [value = json.get(testObj,var)]
     <!-- use set to dynamically create variables with the same names as the keys-->
     [set(var,value)]
}]

Output:<br>
Variable 1: [alpha]<br>
Variable 2: [bravo]<br>
Variable 3: [charlie]<br>
Variable 4: [delta]<br>
Variable 4: [echo]<br>

bobthedog
Cave Troll
Posts: 85
Joined: Sun Aug 03, 2008 7:17 pm

Re: req: JSON for Maptool tutorial

Post by bobthedog »

Rumble wrote:Here's something I just thought of. Call it a varsFromJSON hack:

Code: Select all

<!-- Create Test JSON -->
[h:testObj = '{"alpha":"1d10", "bravo":6, "charlie":"foo", "delta":"boogers", "echo":99}']

<!-- Generate list of fields in the JSON object -->
[h:varList=json.fields(testObj)]

<!-- loop through that list -->
[h,foreach(var,varList),CODE:
{
     <!--get the value at each key in the list-->
     [value = json.get(testObj,var)]
     <!-- use set to dynamically create variables with the same names as the keys-->
     [set(var,value)]
}]

Output:<br>
Variable 1: [alpha]<br>
Variable 2: [bravo]<br>
Variable 3: [charlie]<br>
Variable 4: [delta]<br>
Variable 4: [echo]<br>
Nicely done. And now I'm kicking myself cause I never noticed the "set" function before...

Post Reply

Return to “Documentation Requests/Discussion”