Describe the bug
When you have JSON objects that make up a JSON Array ala [{ },{ }] retrieving an object strips the quotes from the name/values. Example
[h:aspects=json.path.read(json,k+".PowerAspects")]
[h:broadcast(aspects)]
[h:broadcast(json.type(aspects))]
[h:aspect=json.path.read(json,k+".PowerAspects.[0]")]
[h:broadcast(aspect)]
[h:broadcast(json.type(aspect))]
[h:json=json.path.set(json,k+".PowerAspects.[0].DisplayKey",first)]
[h:aspect=json.path.read(json,k+".PowerAspects.[0]")]
[h:broadcast(aspect)]
[h:broadcast(json.type(aspect))]
Produces the following output, not the lack of quotes around the returned JSON object names and values.
[{"DisplayKey":"Boost/Lower Trait","Summary":"Increases or decreases a skill or attribute.","DisplaySummary":"Increases or decreases a skill or attribute.","Rank":"Novice","PowerPoints":2,"Range":"Smarts","AoEFlag":0,"Duration":5,"DurationUnits":"Rounds","DurationRounds":5,"Damage":"","ResistTrait":"","PowerModifiers":[]}]
ARRAY
{DisplayKey=Boost/Lower Trait, Summary=Increases or decreases a skill or attribute., DisplaySummary=Increases or decreases a skill or attribute., Rank=Novice, PowerPoints=2, Range=Smarts, AoEFlag=0, Duration=5, DurationUnits=Rounds, DurationRounds=5, Damage=, ResistTrait=, PowerModifiers=[]}
UNKNOWN
{DisplayKey=Boost Trait, Summary=Increases or decreases a skill or attribute., DisplaySummary=Increases or decreases a skill or attribute., Rank=Novice, PowerPoints=2, Range=Smarts, AoEFlag=0, Duration=5, DurationUnits=Rounds, DurationRounds=5, Damage=, ResistTrait=, PowerModifiers=[]}
UNKNOWN
To Reproduce
Steps to reproduce the behavior:
Expected behavior
The results of using json.path.read to return an array element should return a valid JSON object if that is what is stored in the array
Screenshots
MapTool Info
Desktop (please complete the following information):
Additional context
What are k and first in your example?
I think the problem is probably related to my fix to this: https://github.com/RPTools/maptool/issues/612#issuecomment-531581513
There got to be a way to keep the parenthesis intact.
I can't reproduce your issue.
I use the following json:
[{"Bolt": {
"DisplayKey": "Bolt",
"Rank": "Novice",
"PowerAspects": [
{
"DisplayKey": "Bolt",
"Summary": "2d6 ranged attack.",
"DisplaySummary": "2d6 ranged attack.",
"Rank": "Novice",
"PowerPoints": 1,
"Range": "Smarts",
"AoEFlag": 0,
"Duration": 1,
"DurationUnits": "Rounds",
"DurationRounds": 1,
"Damage": "",
"ResistTrait": "",
"PowerModifiers": []
}
]
}},
{"BoostLowerTrait": {
"DisplayKey": "Boost/Lower Trait",
"Rank": "Novice",
"PowerAspects": [
{
"DisplayKey": "Boost/Lower Trait",
"Summary": "Increases or decreases a skill or attribute.",
"DisplaySummary": "Increases or decreases a skill or attribute.",
"Rank": "Novice",
"PowerPoints": 2,
"Range": "Smarts",
"AoEFlag": 0,
"Duration": 5,
"DurationUnits": "Rounds",
"DurationRounds": 5,
"Damage": "",
"ResistTrait": "",
"PowerModifiers": []
}
]
}}
]
with the following code:
[h:k=""][h:first="Boost Trait"]
[h:aspects=json.path.read(json,k+".PowerAspects")]
[h:broadcast(aspects)] [h:broadcast(json.type(aspects))]
[h:aspect=json.path.read(json,k+".PowerAspects.[0]")]
[h:broadcast(aspect)] [h:broadcast(json.type(aspect))]
[h:json=json.path.set(json,k+".PowerAspects.[0].DisplayKey",first)] [h:aspect=json.path.read(json,k+".PowerAspects.[0]")]
[h:broadcast(aspect)][h:broadcast(json.type(aspect))]
And my output is:
[[{"DisplayKey":"Bolt","Summary":"2d6 ranged attack.","DisplaySummary":"2d6 ranged attack.","Rank":"Novice","PowerPoints":1,"Range":"Smarts","AoEFlag":0,"Duration":1,"DurationUnits":"Rounds","DurationRounds":1,"Damage":"","ResistTrait":"","PowerModifiers":[]}],[{"DisplayKey":"Boost/Lower Trait","Summary":"Increases or decreases a skill or attribute.","DisplaySummary":"Increases or decreases a skill or attribute.","Rank":"Novice","PowerPoints":2,"Range":"Smarts","AoEFlag":0,"Duration":5,"DurationUnits":"Rounds","DurationRounds":5,"Damage":"","ResistTrait":"","PowerModifiers":[]}]]
ARRAY
[{"DisplayKey":"Bolt","Summary":"2d6 ranged attack.","DisplaySummary":"2d6 ranged attack.","Rank":"Novice","PowerPoints":1,"Range":"Smarts","AoEFlag":0,"Duration":1,"DurationUnits":"Rounds","DurationRounds":1,"Damage":"","ResistTrait":"","PowerModifiers":[]},{"DisplayKey":"Boost/Lower Trait","Summary":"Increases or decreases a skill or attribute.","DisplaySummary":"Increases or decreases a skill or attribute.","Rank":"Novice","PowerPoints":2,"Range":"Smarts","AoEFlag":0,"Duration":5,"DurationUnits":"Rounds","DurationRounds":5,"Damage":"","ResistTrait":"","PowerModifiers":[]}]
ARRAY
[{"DisplayKey":"Boost Trait","Summary":"2d6 ranged attack.","DisplaySummary":"2d6 ranged attack.","Rank":"Novice","PowerPoints":1,"Range":"Smarts","AoEFlag":0,"Duration":1,"DurationUnits":"Rounds","DurationRounds":1,"Damage":"","ResistTrait":"","PowerModifiers":[]},{"DisplayKey":"Boost Trait","Summary":"Increases or decreases a skill or attribute.","DisplaySummary":"Increases or decreases a skill or attribute.","Rank":"Novice","PowerPoints":2,"Range":"Smarts","AoEFlag":0,"Duration":5,"DurationUnits":"Rounds","DurationRounds":5,"Damage":"","ResistTrait":"","PowerModifiers":[]}]
ARRAY
I'll send you a PM with a link to the full object.
Could you make a shorter example that works?
yes. It will be a bit.
Here is a simple code snippet that shows the issue.
[h: m = '[{"name":"Troll","HP":75,"Attacks":["Claw","Claw","Bite"]},{"name":"Orc","HP":13,"Attacks":["Sword","Punch"]}]']
[r: json.path.read(m, "[0]")]
Output:
{name=Troll, HP=75, Attacks=["Claw","Claw","Bite"]}
Thanks for the example.
In jayway, indefinite paths always returns a list instead of a json string. I'll have to figure something out.
Why are there even equals signs instead of colons? Shouldn't the element returned be a JSON Object that can be further processed as such?
@Merudo Is there a different way to express the path that does work?
I think switching json provider will work, the default one behaves strangely.
PR #776 should fix this and hopefully not cause any more issues.
The code from @Phergus now returns
{"name":"Troll","HP":75,"Attacks":["Claw","Claw","Bite"]}
I tested the examples on the json.path.read wiki page and they give the same results as they did before.
Working as expected now.
Oops. Law of Unintended Consequences strikes again. String elements are being returned with quotes wrapped around them.
[h:orc = json.set("{}", "name", "Orc", "HP", 13, "Attacks", json.append("Sword", "Punch"))]
Path Read: [r: json.path.read( monsters, "Attacks.[1]")]<br>
JSON Get: [r: atck = json.get(json.get(orc,"Attacks"),1)]
Returns:
Path Read: "Punch"
JSON Get: Punch
Fix may be held till 1.5.8 release.
Fixed by PR #881