[MapTool] JSON Tutorials

Doc requests, organization, and submissions

Moderators: dorpond, trevor, Azhrei

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: [MapTool] JSON Tutorials

Post by Azhrei »

Initializing an empty JSON and appending to a JSON Array are both covered on the wiki page for Wiki: json.set(). Check out the examples, then compare them to the syntax described at the top of the page...

Raphael diSanto
Kobold
Posts: 7
Joined: Sun Oct 17, 2010 10:38 am

Re: [MapTool] JSON Tutorials

Post by Raphael diSanto »

I must be missing something. I don't see anything on that page that describes what you're saying.

There's three examples, and they all put data into the initial array.

Maybe I'm just not understanding the way the documentation is written. Also, that page doesn't mention json.append at all. I guess I don't need to use it?

edit:

Okay. I think I figured it out, via trial and error.

Code: Select all

[h:allDice = '[]']
That initializes an empty array. The documentation said "You can use an empty string ("") to represent a new JSON Array or JSON Object.", but when I did that, I got a new array where the first item was an empty string.

Then, I can make sequential calls like:

Code: Select all

[h:allDice=json.append(allDice,"some value")]
to add values as I loop through my loop.

Sorry for all the confusion. It's a wiki, I'm guessing it's okay if I go add a line or two to clarify the whole.. 'how to predeclare and initialize an empty array' thing?

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

Re: [MapTool] JSON Tutorials

Post by Rumble »

It is okay to edit the wiki. Basically, when "pre-declaring" or initializing a JSON array without adding anything to it when you create it, you can say:

Code: Select all

[h:array = ""]
or

Code: Select all

[h:array = "[]"]
The preceding two examples just say "there is a variable 'array' with that value." Later, when you use the json.append() function, it will see those values and say "ah, okay, you intend those to be blank JSON arrays and all is well with the world."

If you want to initialize it and add things to it all at once, you can instead do:

Code: Select all

[h:array = json.append("", 1, 2, 3, "a", 9, "q", "meatball")]
or

Code: Select all

[array = json.append("[]", 1, 2, 3, "a", 9, "q", "meatball")]
In the immediately preceding two examples, both the "" and the "[]" tell the function to create a new JSON array and at the same time, add those 7 elements to it.

Raphael diSanto
Kobold
Posts: 7
Joined: Sun Oct 17, 2010 10:38 am

Re: [MapTool] JSON Tutorials

Post by Raphael diSanto »

Rumble wrote:It is okay to edit the wiki. Basically, when "pre-declaring" or initializing a JSON array without adding anything to it when you create it, you can say:

Code: Select all

[h:array = ""]
or

Code: Select all

[h:array = "[]"]
The preceding two examples just say "there is a variable 'array' with that value." Later, when you use the json.append() function, it will see those values and say "ah, okay, you intend those to be blank JSON arrays and all is well with the world."
Thank you! .. Yes, that's what I figured out, by trial and error. Is there any difference between them at all?

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

Re: [MapTool] JSON Tutorials

Post by Rumble »

Raphael diSanto wrote:
Rumble wrote:It is okay to edit the wiki. Basically, when "pre-declaring" or initializing a JSON array without adding anything to it when you create it, you can say:

Code: Select all

[h:array = ""]
or

Code: Select all

[h:array = "[]"]
The preceding two examples just say "there is a variable 'array' with that value." Later, when you use the json.append() function, it will see those values and say "ah, okay, you intend those to be blank JSON arrays and all is well with the world."
Thank you! .. Yes, that's what I figured out, by trial and error. Is there any difference between them at all?


I can think of one offhand: if you want to see what type of JSON thing it is - an array or an object - the double quotes won't come back as a known JSON type, while "[]" will.

That is, there are some functions like Wiki: json.type() that tell you whether a particular variable is a JSON Array, a JSON Object, or an "unknown" type (e.g., not JSON anything) - this is sometimes important in various macro techniques. So, i you use, for instance:

Code: Select all

[h:someVariable = ""]
[r:json.type(someVariable)]
it returns UNKNOWN (even though you could use "" when creating a JSON array, the json.type function doesn't recognize it as one otherwise).

As a result, I try to always use "[]" to designate an empty JSON Array, just to keep things clear in my own head.

User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Re: [MapTool] JSON Tutorials

Post by Irrlicht »

I'm bringing to life this topic again, first to thank for the past help, primarily from Rumble; and second, for some more help request.

Short question: how can I build a dialog with a switch field that picks its options from the keys of a json object (given that thay are variable in number and name, since the json object is a token property and its content varies from token to token)?

Explanation: the json obect mentioned above is a storage for weapons; each key is a weapon name, and each corresponding value is a nested json object containing all the properties of the weapon. I'm trying to build a macro (applied to selected token) that displays a dialog where the user can choose from the list of weapons stored in the already mentioned json object/property, and I can't figure out how to have the appropriate number of entries (with their appropriate names, obviously) in the dialog for the selected token.
After the choice is made and the operation confirmed, a series of commands shall be executed (these are the same for any option chosen, but of course all the final output changes accordingly to the values of the chosen weapon.
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

Kurghoth
Kobold
Posts: 15
Joined: Fri Aug 22, 2014 4:36 am
Location: Ancona, Italy
Contact:

Re: [MapTool] JSON Tutorials

Post by Kurghoth »

As long as English is not my first language It's really hard for me to get into JSON things... so I wonder if a little example based on what I need could help me understand all the whole thing. That's what I need to do.

In my game I have a few different races that have different bonuses when you create a character.

Let's say for example that you have:

human
Str +5
Cos +2
Dex +3

dwarf
Str +3
Cos +5
Dex +2

that's what i tried (and does not work)

Code: Select all

[h: human = json.set("{}", "Str", 5, "Cos", 2, "Dex", 3)]
[h: dwarf = json.set("{}", "Str", 3, "Cos", 5, "Dex", 2)]

[h: input("race|human,dwarf|Choose your race|LIST| VALUE=STRING")]  

<h1><b>[r: race]</b></h1>
<h2>Modifier</h2><br>

Str: [r: json.get(race, "Str")] <br>
Cos: [r: json.get(race, "Cos")] <br>
Dex: [r: json.get(race, "Dex")] <br>
There is obviously something I missunderstood. When I choose my race Json.get don't recognise "race" as a variable... I guess it search for an array named "race" that doesn't exist. Can anyone lead me to the correct syntax to achieve this? So I can make the "bigger work" on my own... withouth bothering you all :D

User avatar
JML
Dragon
Posts: 515
Joined: Mon May 31, 2010 7:03 am
Location: Blagnac, France

Re: [MapTool] JSON Tutorials

Post by JML »

The reason is your "race" variable is defined in the Wiki: input() instruction as the receiving variable, meaning it's just a string, as implied by the VALUE=STRING. Thus you're trying to do a Wiki: json.get() on a string variable, not a JSON one.

Maybe something along:

Code: Select all

Str: [r: json.get(eval(race), "Str")]
…could do the trick?

Kurghoth
Kobold
Posts: 15
Joined: Fri Aug 22, 2014 4:36 am
Location: Ancona, Italy
Contact:

Re: [MapTool] JSON Tutorials

Post by Kurghoth »

JML wrote:…could do the trick?
Thank you so much... I know something new... this way work perfectly... I apreciate the help ^_^

Post Reply

Return to “Documentation Requests/Discussion”