I am writing a macro to populate a library of weapons on a library token. The weapons library is a property of nested Json objects. I am getting this error: "Error with function "json.path.put": Can only add properties to a map" Help with this please? I am also getting the following error: "Error with function "json.path.put": Use bracket notion ['my prop'] if your property contains blank characters. position: 2". I figured out it was a space in Name variable. How do I rewrite this so I can include spaces in a property?
[h: WeaponsLibrary = getProperty("WeaponsLibrary","Lib:token","Data")]
[h: abort(input("Name||Enter Name|TEXT","SRange||Enter Short Range|TEXT","MRange||Enter Medium Range|TEXT","LRange||Enter Long Range|TEXT","NumDMG|1|Enter Number of Damage Dice|TEXT","DMGDie|4|Enter Damage Die Type|TEXT","DMGMod||Enter Damage Modifier|TEXT","AP||Enter AP Rating|TEXT","RoF||Enter Rate of Fire|TEXT","MaxShots||Enter Ammo Capacity|TEXT","MinStr||Enter Minimum Strength|TEXT","WT||Enter Weight|TEXT","Cost||Enter Cost|TEXT","Parry||Enter Parry Value|TEXT","Hand|2|Enter Number of Hands|TEXT"))]
[h: AddWeaponsLibrary = json.set("{}",Name,"Name")]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"SRange",SRange)]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"MRange",MRange)]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"LRange",LRange)]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"NumDMG",NumDMG)]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"DMGDie",DMGDie)]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"DMGMod",DMGMod)]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"AP",AP)]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"RoF",RoF)]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"MaxShots",MaxShots)]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"MinStr",MinStr)]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"WT",WT)]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"Cost",Cost)]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"Parry",Parry)]
[h: AddWeaponsLibrary = json.path.put(AddWeaponsLibrary,Name,"Hand",Hand)]
[r: WeaponsLibrary = if(json.contains(WeaponsLibrary,AddWeaponsLibrary),WeaponsLibrary,json.merge(WeaponsLibrary,AddWeaponsLibrary))]
[h: setProperty("WeaponsLibrary",WeaponsLibrary,findToken("Lib:token","Data"),"Data")]
Help with Json Path error
Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice
Re: Help with Json Path error
Maybe try an array of objects. Works better with the json.path functions.
Untested
But you could just use an object of objects where the name of weapon is the key. You wouldn't use the json.path functions then, just normal json.get and json.set. The power of using json.path is if you want to retrieve a list of weapons with certain criteria like all weapons with SRange value == 10 or whatever. If that's something you're not doing, I would avoid using json.path.put and just use json.set.
Untested
Code: Select all
<!-- just for testing, WeaponsLibrary should be an array of weapon objects -->
[H: WeaponsLibrary = "[]"]
[h: weaponObj = json.set("{}",
"Name",Name,
"SRange",SRange,
"MRange",MRange,
"LRange",LRange,
"NumDMG",NumDMG,
"DMGDie",DMGDie,
"DMGMod",DMGMod,
"AP",AP,
"RoF",RoF,
"MaxShots",MaxShots,
"MinStr",MinStr,
"WT",WT,
"Cost",Cost,
"Parry",Parry,
"Hand",Hand
)]
<!-- test for existing weapon -->
[H: found = json.path.read(WeaponsLibrary,strformat(".[?(@.Name == '%{Name}')]"))]
[H, if(json.isEmpty(found)), code: {
[H: WeaponsLibrary = json.append(WeaponsLibrary,weaponObj)]
};{
<!-- weapon already exists, replace it -->
[H: index = json.indexOf(WeaponsLibrary,found)]
[H: WeaponsLibrary = json.set(WeaponsLibrary,index,weaponObj)]
}]
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: Help with Json Path error
That worked brilliantly for adding them but I tried re-entering one a second time to see how the update part worked and got this error message:
java.lang.IndexOutOfBoundsException: Index -1 out-of-bounds for length 23 error executing expression WeaponsLibrary = json.set(WeaponsLibrary,index,WeaponObj).
I think that means when it finds the existing entry by name (found), it is giving it an index -1. That implies "found" does not exist but that would imply it should add a duplicate entry. The fact that it jumped to the false condition of json.isEmpty(found) means that found has a value which was identified. This error seems to be contradictory.
java.lang.IndexOutOfBoundsException: Index -1 out-of-bounds for length 23 error executing expression WeaponsLibrary = json.set(WeaponsLibrary,index,WeaponObj).
I think that means when it finds the existing entry by name (found), it is giving it an index -1. That implies "found" does not exist but that would imply it should add a duplicate entry. The fact that it jumped to the false condition of json.isEmpty(found) means that found has a value which was identified. This error seems to be contradictory.
- bubblobill
- Giant
- Posts: 167
- Joined: Sun Jan 24, 2010 3:07 pm
Re: Help with Json Path error
There is an error in the index formula, is should be checking for weaponObj not found.
Code: Select all
[H: found = json.path.read(WeaponsLibrary,strformat(".[?(@.Name == '%{Name}')]"))]
[H, if(json.isEmpty(found)), code: {
[H: WeaponsLibrary = json.append(WeaponsLibrary,weaponObj)]
};{
<!-- weapon already exists, replace it -->
[H: index = json.indexOf(WeaponsLibrary,weaponObj)]
[H: WeaponsLibrary = json.set(WeaponsLibrary,index,weaponObj)]
}]
Bubblobill on the forum.
@Reverend on the MapTool Discord Server
Responsible for less atrocities than most... I do accept responsibility for these though: SmartDoors, Simple d20 Framework - barebones, Drop-in: All 3.5 SRD Monsters, Drop in: Simple Character Editor, Battletech Framework
@Reverend on the MapTool Discord Server
Responsible for less atrocities than most... I do accept responsibility for these though: SmartDoors, Simple d20 Framework - barebones, Drop-in: All 3.5 SRD Monsters, Drop in: Simple Character Editor, Battletech Framework
Re: Help with Json Path error
Yup, that did it.
Re: Help with Json Path error
Hm, that will only work if the objects are the same. Perhaps my found path isn't correct, still new to that. Maybe adding [*] to the end will return the object in that array position.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
- bubblobill
- Giant
- Posts: 167
- Joined: Sun Jan 24, 2010 3:07 pm
Re: Help with Json Path error
json.indexOf will only work for the object against WeaponsLibrary.
found should return the stored object with the appropriate name.
You would then set that object to whatever your updated values are.
found should return the stored object with the appropriate name.
You would then set that object to whatever your updated values are.
Bubblobill on the forum.
@Reverend on the MapTool Discord Server
Responsible for less atrocities than most... I do accept responsibility for these though: SmartDoors, Simple d20 Framework - barebones, Drop-in: All 3.5 SRD Monsters, Drop in: Simple Character Editor, Battletech Framework
@Reverend on the MapTool Discord Server
Responsible for less atrocities than most... I do accept responsibility for these though: SmartDoors, Simple d20 Framework - barebones, Drop-in: All 3.5 SRD Monsters, Drop in: Simple Character Editor, Battletech Framework