jsonPath filter In Maptool 1.6.1

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

Moderators: Azhrei, dorpond, trevor

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
stolkun
Kobold
Posts: 9
Joined: Fri Mar 27, 2020 4:30 pm

jsonPath filter In Maptool 1.6.1

Post by stolkun »

Hello there, so i was using json.path.read to search through my datas but i had a problem with the filter In that i tried to use:
[h: res = json.path.read(notes, "\$.[?(@.Classe == '" + class + "' && '" + school + "' in @." + type + " && @.Niveau <= " + lvl + ")]")]
which correspond to for example :
[h: res = json.path.read(notes, "\$.[?(@.Classe == 'Pretre' && 'S' in @.Sphere && @.Niveau <= 4)]")]
The part with Classe and Niveau works well but the filter In doesn't give out anything.
Just so you know an example of field Sphere that i have is "S" or "[S,H]"
So i was wondering if anyone had an idea of how the filter in works or if it is implemented.
Thanks in advance

User avatar
aliasmask
Deity
Posts: 8579
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: jsonPath filter In Maptool 1.6.1

Post by aliasmask »

When playing around with "in" I get this error

Code: Select all

Error with function "json.path.read": com.google.gson.JsonArray cannot be cast to java.base/java.util.List
So, I'm thinking the @.key in List part has to be built in to the read as @.Sphere in ['S','H'] and not the other way around. I tried using a json array, but I got the error above.

I used this code to help test.

Code: Select all

[H: records = json.append("",json.set("{}","key1",1,"key2","1,2"))]
[dialog("D"):{<pre>[R: json.indent(records)]</pre>}]

[r: test = json.path.read(records,"\$.[*][?(@.key1 in [1,2])]")]
[r: test = json.path.read(records,"\$.[*][?(@.key1 in @.key2)]")]
I tried the value of key2 as a list and json array. At least with list it doesn't give me an error, but it doesn't find the match and returns [], but the first one does return a match.

stolkun
Kobold
Posts: 9
Joined: Fri Mar 27, 2020 4:30 pm

Re: jsonPath filter In Maptool 1.6.1

Post by stolkun »

Ye i tried what you said and you're right, I hope one day we will be able to research like a regex with json path read thanks ^^

Phergus
Deity
Posts: 7108
Joined: Fri May 12, 2006 8:56 pm
Location: Middle of Nowhere, NM
Contact:

Re: jsonPath filter In Maptool 1.6.1

Post by Phergus »

Here's an example based on the info for the json path library

Code: Select all

[h: j='{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, "expensive": 10 }']
[h:q="store.book[?(@.category in ['fiction'])]"]
[h: res = json.path.read(j, q)]
<pre>[r: json.indent(res,2)]</pre>
Which results in the 3 books that are "fiction".

Code: Select all

[
    {
    "category": "fiction",
    "author": "Evelyn Waugh",
    "title": "Sword of Honour",
    "price": 12.99
  },
    {
    "category": "fiction",
    "author": "Herman Melville",
    "title": "Moby Dick",
    "isbn": "0-553-21311-3",
    "price": 8.99
  },
    {
    "category": "fiction",
    "author": "J. R. R. Tolkien",
    "title": "The Lord of the Rings",
    "isbn": "0-395-19395-8",
    "price": 22.99
  }
]

stolkun
Kobold
Posts: 9
Joined: Fri Mar 27, 2020 4:30 pm

Re: jsonPath filter In Maptool 1.6.1

Post by stolkun »

Ye i resolved my problem by using the regex of jsonpath cause using a list in my @.Sphere doesn't work with the filter in ^^ Thanks a lot

Post Reply

Return to “MapTool”