Struggling with Json

If you have searched the other User Creations subforums and an answer to your question has not been found, please post here!

Moderators: dorpond, trevor, Azhrei, Gamerdude

Post Reply
User avatar
RedDog
Dragon
Posts: 393
Joined: Sat Jan 05, 2008 10:02 pm
Location: Clearwater, FL

Struggling with Json

Post by RedDog »

I have been working on a d20 campaign framework off and on for some a number of years as a sort of hobby project. When Json was introduced, I decided not to jump on the band wagon as it seamed that there was not a huge benefit to converting my previous work over.

A while back I started to wonder if I was missing some helpful functionality with Json, so I started working with various bits of test scripts to try and get a grip on how things work. I found the json tutorials and wiki pages on the RPTools Wiki. Despite that, I kept running into wall after wall after wall where things did not work as I expected.

One of the challenge I have been hitting the wall with is that I do not know what functionality json can provide. Also, how should I form the data structure so I can take advantage of the functionality that does exist. From looking at Json in general, I can't help but think that it can be object oriented so I can add and subtract whole datasets, but I have not been able to make it work.

It is difficult to figure out what questions to ask because I just cannot get a grip on how things should work. I am hoping someone can give me some direction, tutorials, or work with me on some sample code.

To start, I would like to come up with a data structure I know is valid.
Secondly, I am hoping to confirm that my assumptions are correct or not on how Json can be used.
I am not worried about the specific use of Json functions at this point as I can delve into the how-to later on.

Here is a sample I worked up for a variable called "SaveArray" if someone is willing to work with me on it. I know the formatting is not consistent, but I hope it makes it more readable...

Code: Select all

{   "SavingThrow":"Fortitude": {
        "Class":"Paladin": {
           "ClassBase" : {"Desc":"Base", "Type":"None", "Value":4 },
           "ClassMod": { "Desc":"Divine Grace", "Type":"None", "Value":2 }         
           },
        "RaceMod" : {"Desc":"Dwarf", "Type":"Racial", "Value":2 },
        "AbilityMod" : {"Desc":"Constitution", "Type":"ConMod", "Value":3 }
        "ItemMod": { "Desc":"Ring of Protection +1", "Type":"Deflection", "Value":1 },
        "ItemMod": { "Desc":"Luck Blade +2", "Type":"Luck", "Value":1 },
        "MagicMod": { "Desc":"Guidence", "Type":"Competence", "Value":1 }
        },
    "SavingThrow":"Reflex": {
        "Class":"Paladin": {
           "ClassBase" : {"Desc":"Base", "Type":"None", "Value":1 },
           "ClassMod": { "Desc":"Divine Grace", "Type":"None", "Value":2 }         
           },
        "RaceMod" : {"Desc":"Dwarf", "Type":"Racial", "Value":0 },
        "AbilityMod" : {"Desc":"Dexterity", "Type":"DexMod", "Value":-1 }
        },
    "SavingThrow":"Will": {
        "Class":"Paladin": {
           "ClassBase" : {"Desc":"Base", "Type":"None", "Value":1 },
           "ClassMod": { "Desc":"Divine Grace", "Type":"None", "Value":2 }         
           },
        "RaceMod" : {"Desc":"Dwarf", "Type":"Racial", "Value":0 },
        "AbilityMod" : {"Desc":"Wisdom", "Type":"WisMod", "Value":0 }
        }
1) I assume Json allows me to treat the various internal items like "ClassBase", "ItemMod, "MagicMod"" and the accompanying values as objects I can update, add, or remove including objects with the same name.
2) I am also assuming that I am able to have multiple objects with the same name within the same structure such as "ItemMod", as long as I recursively check for more than one instance of these objects.
3) The structure I chose above was intended to allow me to have a single variable called SavingThrow Array that contains all of the data that affects a saving throw. Then, if an item is equipped or a spell is cast on a character, I can add a new internal object to the appropriate Saving Throw so it is ready when the player needs to make a roll. Is this a good way to set it up or is there a better way?

A few other questions:
1) Is it better or worse to have the values of the objects in quotes, eg "Value":"1".
2) When enclosing strings, is there any benefit (visually or functionally) whatsoever to using quotes vs apostrophes?

This whole conundrum on whether to use Json or not has essentially stopped me from continuing work on my campaign framework completely.
I am pretty sure me asking for help on this is a pretty big 'ask', so any assistance at all is really appreciated.

Jmr3366
Cave Troll
Posts: 81
Joined: Fri Feb 04, 2022 1:08 pm

Re: Struggling with Json

Post by Jmr3366 »

it was a bit TLDR but I'd suggest understanding the basic format requirements. https://developer.mozilla.org/en-US/doc ... jects/JSON
I'm certain others have better resources to share.
I'd also suggest digging into some macros out there so you can see how they use json. There are a ton of different ways people use it.

User avatar
aliasmask
RPTools Team
Posts: 9023
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Struggling with Json

Post by aliasmask »

There are many tools for manipulating jsons, most of which start with "json." in the function library. I recommend building your jsons using json.append for arrays and json.set for objects rather than some direct string manipulation. For organization, I like to use "record" format which is an array of objects where all the objects have the same keys. json.path.read is a good tool to get deeper structures of a json dataset. I wrote a function:

Code: Select all

<!-- record.index(records,key,value): index
   records - A json array of objects where the objects have the same keys in each array element
   key - Main key name that makes record unique. The value of key is unique.
   value - value to find index of   
   index - where in the record this key/value can be found. returns "" if not found
   
AM 11-25-21
   This function will return the index of a set of records where a key/value combo can be found.
   
-->

[H: macro.return = replace(json.path.read(arg(0),strformat("\$[?(@.['%s'] == '%s')]",arg(1),arg(2)),"ALWAYS_RETURN_LIST,AS_PATH_LIST,SUPPRESS_EXCEPTIONS"),"[^0-9]","")]
and several other json related functions to help with json structures, so if you have specific questions you can post them in Discord and me or someone else will get to it pretty quickly.

There is no benefit to adding quotes around numbers. Treating numbers as strings is mostly an output thing.
The quote vs squote is fairly interchangeable, but I predominately use quotes. But I think this is hinting at building jsons manually, which I don't recommend. Use the json functions.

User avatar
RedDog
Dragon
Posts: 393
Joined: Sat Jan 05, 2008 10:02 pm
Location: Clearwater, FL

Re: Struggling with Json

Post by RedDog »

Thank you Jmr3366 and aliasmask. I will work with it a bit and come back with specific examples if/when I hit another roadblock.

Post Reply

Return to “Requests for HELLLLP!”