Episode 2: Revenge of the Json (aka here i go again)

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
User avatar
aku
Dragon
Posts: 856
Joined: Wed Nov 15, 2006 9:03 am
Contact:

Episode 2: Revenge of the Json (aka here i go again)

Post by aku »

So, a few years ago, i had begun fiddling with JSON for a framework for a different game system (Ars Magica for those of you with long term memory problems).

This time around, it's Shadowrun 4th edition, and I'm picking up work where I had left off. I have a working dialog box, for adding skills and ranks to a character. Works lovely, However, I want to add another field to include the skill set that it's on (so I can do some convoluted stuff with it next, like decide from different macros for various things to roll dice on, particularly combat or non combat type stuff.) Add the drop down menu, and after a few wrenches, got it to put the drowdown menu for me. That looks like this:

Code: Select all

<!-- addSkill -->
[h: status = input(
  "skillName|skill|Skill",
  "skillRank|0|Rank",
"skillSet|Combat Active,Magical Active,Physical Active,Resonance Active,Social Active,Technical Active,vehicle Active,Street Knowledge,Academic Knowledge,Professional Knowledge,Interests Knowledge,Language Knowledge,|Skill Group|LIST|SELECT=0 VALUE=STRING"
)]
[h: abort(status)]
[h: assert(isNumber(skillRank), "You must enter a number for skill rank!")]
However, when i try to add skillSet to the JSON, which works looking like this:

Code: Select all

[PCSkillsJson = json.set(PCSkillsJson, skillName,skillRank)]
It throws an "error in body" message. to clerify, if i have skillName,skillRank,skillSet, it throws an error, as soon as i deleted skillSet, it bugs out. I have already re-ran my onCampaignLoad macro, and the initialize JSON, thinking maybe because I had already established that json object, it wasn't validating or something, but that didnt seem to help. going to do a few more things like deleting all skill entries and seeing if that fixes, but any other ideas?

User avatar
CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: Episode 2: Revenge of the Json (aka here i go again)

Post by CoveredInFish »

Hmm ... I dont get the actual problem unless its a misunderstanding of the basic json data structure.

json.set() will make a json.object that only can hold key:value pairs. It cannot "automatically" assign multiple values to a single key.

You can nest json.objects, so you could do something like this.

Code: Select all

[PCSkillsJson = json.set(PCSkillsJson, skillName,json.set("", "name", skillName, "rank", skillRank, "set", skillSet))]
This way you store all information and can retrieve it, though it takes a step more to get to the data you want.

Code: Select all

[h: skill = json.get(PCSkillsJson, "Shooting")]
[h: rank = json.get(skill, "rank")]

User avatar
aku
Dragon
Posts: 856
Joined: Wed Nov 15, 2006 9:03 am
Contact:

Re: Episode 2: Revenge of the Json (aka here i go again)

Post by aku »

CoveredInFish wrote:Hmm ... I dont get the actual problem unless its a misunderstanding of the basic json data structure.
Give that main a nail, cuz he hit it on the head....

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

Re: Episode 2: Revenge of the Json (aka here i go again)

Post by Azhrei »

Check out this JSON editor. I use the online version but you can download it and run it locally (or put it on your own web server).

You can use its GUI interface to build the JSON structure the way you like, then convert each object or array to a corresponding Wiki: json.set() function call. I do the innermost ones first and work my way out from there, but it depends on which way is easier for you to visualize.

User avatar
aku
Dragon
Posts: 856
Joined: Wed Nov 15, 2006 9:03 am
Contact:

Re: Episode 2: Revenge of the Json (aka here i go again)

Post by aku »

I will definitely play with that once i get my feet on solid ground Az, thanks!

But now, for my problem. (or one of them):

I'm trying to create a structure that looks like the attached image. From my understanding, the green PC skills should be what rumbled described as the "parent JSON" in his tutorial here. I can't figure out if this should be an array, or an object. I am thinking object, because not every character will have the same set of skills, entered at the same time, which I think to use an array correctly would require.

The blue text would be the keys, while red being values (and the pink being values and names of the objects).

So far is my logic sound? am I missing something, or am I just struggling with syntax at this point?
Attachments
JSONsetup.jpg
JSONsetup.jpg (18.99 KiB) Viewed 675 times

User avatar
aku
Dragon
Posts: 856
Joined: Wed Nov 15, 2006 9:03 am
Contact:

Re: Episode 2: Revenge of the Json (aka here i go again)

Post by aku »

so, lots of view, anyone have any actual feedback for me? I think i'm struggling with syntax, but would love confirmation

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Episode 2: Revenge of the Json (aka here i go again)

Post by wolph42 »

if all keys are unique you can go for object. If not then array. The advantage of object is that you can directly access the value and you can use json.fields to get the keys.

your picture does not show all, but my guess this could be a possible structure:
pc skills = {ranged combat: {pistols:2, rifles:4}, melee combat: {swords:1, knives:3}}

User avatar
CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: Episode 2: Revenge of the Json (aka here i go again)

Post by CoveredInFish »

I found that neither the picture was informative enough nor was my knowledge of SR4 good enough to propose a data structure.

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Episode 2: Revenge of the Json (aka here i go again)

Post by wolph42 »

CoveredInFish wrote:I found that neither the picture was informative enough nor was my knowledge of SR4 good enough to propose a data structure.
i actually had the exact same issue, which in retrospect we should have said immediately. This time I chose to do what you did last time :wink:

Post Reply

Return to “Macros”