WorkMaze / JUST.net

JUST - JSON Under Simple Transformation (XSLT equivalent for JSON).
MIT License
171 stars 54 forks source link

string contains inside loop #233

Open anuradharajan opened 2 years ago

anuradharajan commented 2 years ago

{ "Array" [ { "Id" : "123AAA"}, { "Id" : "123BBB"}, { "Id" : "123AAA"} ] }

I want to loop through the Array objects that contain Id value = "AAA".

loop(???)

Courela commented 2 years ago

I only can see this being done by JsonPath, if you're using JsonPath, although Newtonsoft Json does not provide a "contains"/regex operator like other languages do (operator ~=). Maybe it's possible to do this with JmesPath instead of JsonPath, if that's an option.

anuradharajan commented 2 years ago

Could you provide me with how that transform will look? JsonPath or JmesPath

Courela commented 2 years ago

With JsonPath, the closest that I can think of is using #ifgroup inside the loop, but this will create empty items when the item does not match:

{
  "Array": {
    "#loop($.Array)": { 
      "#ifgroup(#mathgreaterthanorequalto(#firstindexof(#currentvalueatpath($.Id),AAA),0))": {
        "Value": "#currentvalue()"
      }
    }
  }
}

I don't know enough of JmesPath to provide a solution.

Courela commented 2 years ago

It seems there's an operator for regex in Newtonsoft Json after all. https://www.newtonsoft.com/json/help/html/RegexQuery.htm