json obect vs array

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
Sydious
Giant
Posts: 155
Joined: Sun Aug 21, 2011 2:27 am

json obect vs array

Post by Sydious »

I am still struggling with jsons.

I believe this is a json object
{"Sydious":{"pRoll":4},"Tester_01":{"pRoll":1},"Tester_02":{"pRoll":4},"Tester_03":{"pRoll":5}}
When I want to use json.sort, it needs to be a json array.

How do I switch a json object to a json array?

Im trying to sort the order of players based on thier roll.

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

Re: json obect vs array

Post by aliasmask »

What do you want to sort by, key name or key value? You have a nested json object (objects with in an object). If you sort by the key name, then just simply get the list of fields from the object:

Code: Select all

[H: keyList = json.fields(obj,"json")]
[H: sortedList = json.sort(keyList,"a")]
If you want to sort the key list by the value with in the nested object then you do this:

Code: Select all

[H: sortedList = json.sort(obj,"a","pRoll")]
A note about your object. You should try and avoid nesting when you can because of speed issues when saving and passing the object. Since pRoll is the only item in the nested object, is it really necessary? Couldn't you just assign the outside key name to the pRoll value? But perhaps you're just simplifying the example you're giving us.

btw, the sorted list is a json array of the key names, not the actual object.

Sydious
Giant
Posts: 155
Joined: Sun Aug 21, 2011 2:27 am

Re: json obect vs array

Post by Sydious »

I thought that json looked wierd to me.

What i am trying to do is simple I think..(or so I thought....)

I pass a diaolg window to each player. The submit button logs a roll of a d6 and stores it on a Lib:token property called playerRolls.

Then when all the player's rolls are added to playerRolls, i want to sort that list from greatest to smallest and re-set the lib:token property with the new list. Then i will create a list of playerNames and store that in another proprery playerOrder.


My json construction is obviously flawed...
I set the playerRolls in the initiating macro by using:

Code: Select all

[h: setLibProperty("playerRolls", "{}", "Lib:DQ")]
Then when I process each roll, I use the following code to update that property.

Code: Select all

[h: playerRollDetails= json.set("{}", "pRoll", myroll)]
[h: playerRolls = json.set(playerRolls, rollingPlayer, playerRollDetails)]
[h:setLibProperty("playerRolls", playerRolls, "Lib:DQ")] 
as i said, I am still confused how to use jsons but this is how the majority of my jsons are created. I am more going through the motions, then actually fully understanding it. I know there are objects and arrays , but just don't get the diffrence between the two, which to use when, and how to create each type.

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

Re: json obect vs array

Post by aliasmask »

Oh, I was a bit off in my code above. That first level can only be a json array. So, when you add the roll from the player, do it like this:

Code: Select all

[H: playerRolls = json.append(playerRolls,json.set("{}","name",playerName,"roll",pRoll))]
This keeps track of the roll and the player name. You then sort your list:

Code: Select all

[H: sortedPlayers = json.sort(playerRolls,"d","roll")]
The array is now in the order of highest roll to lowest roll. You can now loop through the array to get the names:

Code: Select all

[H: nameList = ""]
[H, foreach(player,sortedPlayers): nameList = json.append(nameList,json.get(player,"name"))]

Sydious
Giant
Posts: 155
Joined: Sun Aug 21, 2011 2:27 am

Re: json obect vs array

Post by Sydious »

How are ties sorted when you use json.sort?
Is there something that is common practice to deal with tie breaks?

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

Re: json obect vs array

Post by aliasmask »

I assume it to be random, but probably the last in original list becomes the first, or first in list is first.. don't know. But you can add more sort options. For example, you can use name as second sort criteria, or you can add another value to json object to help determine tie breakers.. like a dex value or whatever.

Sydious
Giant
Posts: 155
Joined: Sun Aug 21, 2011 2:27 am

Re: json obect vs array

Post by Sydious »

Is there a way to roll decimels?

User avatar
jfrazierjr
Deity
Posts: 5176
Joined: Tue Sep 11, 2007 7:31 pm

Re: json obect vs array

Post by jfrazierjr »

Sydious wrote:as i said, I am still confused how to use jsons but this is how the majority of my jsons are created. I am more going through the motions, then actually fully understanding it. I know there are objects and arrays , but just don't get the diffrence between the two, which to use when, and how to create each type.
Ok... real simple:
  • json objects are key/value pairs. There is no ordering. The where does not matter, when you ask an object for it's "AC"(or whatever) it just gives you back the value if there is one. Thing of this like a "person" object who has keys of
    • "eye color" = "brown"
    • "hair color" = "black"
    • "height" = "5 foot 6 inches"
  • json objects are an ordered list of things. Typically, you would use this if you need to maintain order of something, say for example a person's work history in chronological order.
Now.. the real trick is knowing when to use which and it really depends on context. Going back to the person example, you might have something like:

Code: Select all

{
    "Person": {
        "Name": "Rob",
        "Hair": "Black",
        "Children": [
            "Jill",
            "Henry",
            "Bill"
        ]
    }
}
 


so you have a person object with child objects of Name, Hair, and Children. Name and Hair's values are simple strings, while Children's value is an array. The Children array has an explicit order, but what context is there to say what the ordering means? If you NEED that context or any other information about the children (such as THEIR hair color, eye color, etc, then Children's value would be much better as an object itself for each child or an array. If you want to keep the ordering(say oldest age going down), you would do something like:

Code: Select all

{
    "Person": {
        "Name": "Rob",
        "Hair": "Black",
        "Children": [
            {
                "Name": "Jill",
                "Hair": "blue",
                "Age": 18
            },
            {
                "Name": "Henry",
                "Hair": "Brown",
                "Age": 14
            },
            {
                "Name": "Bill",
                "Hair": "Black",
                "Age": 8
            }
        ]
    }
} 
Now, with this example, you can track the children's ages in order. If for some really odd reason Rob found out he had another child that was 16(bad Rob!!!), you would have to loop over the Children array, check each's Age property and store the previous child's age value and insert the Children into a NEW array with the new child in the correct place.
I save all my Campaign Files to DropBox. Not only can I access a campaign file from pretty much any OS that will run Maptool(Win,OSX, linux), but each file is versioned, so if something goes crazy wild, I can always roll back to a previous version of the same file.

Get your Dropbox 2GB via my referral link, and as a bonus, I get an extra 250 MB of space. Even if you don't don't use my link, I still enthusiastically recommend Dropbox..

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

Re: json obect vs array

Post by aliasmask »

Sydious wrote:Is there a way to roll decimels?
I don't know what you're doing, but I think you're asking the wrong question. The sort does work with decimals. So, sorting in descending order the number 10.2 would be before 10.1.

The 3.5/Pathfinder init is generated with an init roll and adds the dex bonus / 100 to init roll.

Sydious
Giant
Posts: 155
Joined: Sun Aug 21, 2011 2:27 am

Re: json obect vs array

Post by Sydious »

I'm simply having each player roll a D6.

I need to break the ties if they arise.

Now I am thinking I should make 2 rolls, 1d6 and something like 1d99999 and break ties by sorthing by the 1d99999 roll second. I guess there is still the posibility to tie but chances are slim..


IRL i guess the tie would be broken by rerolls until someone wins..

Post Reply

Return to “Macros”