Search code examples
arraysjsonjsonlogic

Extract an element inside an array that is inside a json object using jsonlogic


I have the following JSON

{
    "transactionId" : "123456789",
    "amount" : 215,
    "refunds" : {
       "detail" : [
           {
               "id" : 12345,
               "amount" : 7
           },
           {
               "id" : 67890,
               "amount" : 15
           }
       ],
       "total_amount" : 22
   }
}

I need to extract the information from the refunds node, where the id = 12345 using only jsonlogic.

Exploring the operations supported by jsonlogic, I found that it has an operation for arrays called filter (link), but I have only found examples for arrays of basic data types (int, string, etc) but not for arrays of objects.

Does anyone have any idea how it could be?


Solution

  • Doing different tests I found out that it is possible to filter this information using the filter operation.

    the filter is as follows:

    {"filter":[{"var":"refunds.detail"},{"==":[{"var":".id"},"12345"]}]}
    

    the crucial part is to add before the field to filter the character "."