Import from *.json object?

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: dorpond, trevor, Azhrei

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
User avatar
Mrugnak
Dragon
Posts: 745
Joined: Mon Jul 21, 2008 7:38 pm

Import from *.json object?

Post by Mrugnak »

I have a complex JSON datastructure I would like to get into Maptools. Cutting and pasting it into a Notes field or into a popup or into the command line is fine, I just want to end up with a MapTools JSON object structure that matches my JSON file.

an example
Some Code Here

Code: Select all

{"Axe":{"Type":"melee","Config":[{"Name":"swing","Damage":"2d-1","DamageType":"cut","Skill":15,"Parry":11},{"Name":"thrust","Damage":"1d","DamageType":"imp","Skill":15,"Parry":11}],"Notes":""},"9mm Pistol":{"Type":"ranged","Shots":12,"Config":[{"Name":"9mm ball","Acc":3,"Skill":15,"Damage":"1d+2","DamageType":"pi","Half":100,"Max":250,"ROF":3},{"Name":"9mm hollowpoint","Acc":2,"Skill":15,"Damage":"1d+2","DamageType":"pi+","Half":10,"Max":100,"ROF":3},{"Name":"9mm duplex","Acc":1,"Skill":15,"Damage":"1d-2","DamageType":"pi-","Half":3,"Max":40,"ROF":3,"PerShot":2,"Comment":"rof 3*2"}]}}
I can't just assign this to a variable, as Maptool doesn't "speak" JSON natively. I can't find a way to put this in a string and parse it. Well, I could write a JSON parser in maptool macros, but eeeeegad no I refuse.

Is there some sort of way to parse a JSON object imported as a string?
I can't do this as a StrProp, the structure is multidimensional.

EDIT: I fixed my code example. some derpy stuff due to trying to hand type a short example instead of just cutting and pasting like a smart.

User avatar
Mrugnak
Dragon
Posts: 745
Joined: Mon Jul 21, 2008 7:38 pm

Re: Import from *.json object?

Post by Mrugnak »

I've played a little with the basic js parsing in the dev build, and I can do it that way via some contorted shenanigans. Our group isn't using the dev build though, it's using b91. Any ideas on how to make it work in older Maptools?

For the curious, here's my example on how to get an external JSON datastructure into Maptools. This method relies on cut-and-paste the JSON file into the program somehow - this is to bypass the more complex route of just building the token externally.

Code: Select all

External OBJ: [t:externalJSON = "{'Axe':{'Type':'melee','Config':[{'Name':'swing','Damage':'2d-1','DamageType':'cut','Skill':15,'Parry':11},{'Name':'thrust','Damage':'1d','DamageType':'imp','Skill':15,'Parry':11}],'Notes':''},'9mm Pistol':{'Type':'ranged','Shots':12,'Config':[{'Name':'9mm ball','Acc':3,'Skill':15,'Damage':'1d+2','DamageType':'pi','Half':100,'Max':250,'ROF':3},{'Name':'9mm hollowpoint','Acc':2,'Skill':15,'Damage':'1d+2','DamageType':'pi+','Half':10,'Max':100,'ROF':3},{'Name':'9mm duplex','Acc':1,'Skill':15,'Damage':'1d-2','DamageType':'pi-','Half':3,'Max':40,'ROF':3,'PerShot':2,'Comment':'rof 3*2'}]}}"]<br>
Convert from string to datastructure via Javascript [t:jobj = js.eval("var bob = " + externalJSON + "; return bob")]<br>
Use JSON normally. EG Axe mode 1 damage: [t:json.get(json.get(json.get(json.get(jobj,"Axe"),"Config"),0),"Damage")]
I'm cating the string because using javascript's json.parse requires nesting quotes three layers deep, which we can't do. I see in the dev notes that newer builds can pass in as an arg, so you could do that too.

Post Reply

Return to “MapTool”