Page 2 of 2

Re: [MapTool] JSON Tutorials

Posted: Wed Oct 20, 2010 1:47 pm
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...

Re: [MapTool] JSON Tutorials

Posted: Wed Oct 20, 2010 4:42 pm
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?

Re: [MapTool] JSON Tutorials

Posted: Thu Oct 21, 2010 3:25 pm
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.

Re: [MapTool] JSON Tutorials

Posted: Sun Oct 24, 2010 9:21 am
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?

Re: [MapTool] JSON Tutorials

Posted: Sun Oct 24, 2010 12:01 pm
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.

Re: [MapTool] JSON Tutorials

Posted: Sat Dec 25, 2010 9:31 pm
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.

Re: [MapTool] JSON Tutorials

Posted: Fri Apr 03, 2015 6:26 am
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

Re: [MapTool] JSON Tutorials

Posted: Fri Apr 03, 2015 10:33 am
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?

Re: [MapTool] JSON Tutorials

Posted: Sat Apr 04, 2015 5:58 am
by Kurghoth
JML wrote:…could do the trick?
Thank you so much... I know something new... this way work perfectly... I apreciate the help ^_^