0

I am trying to remove an array object based on value of array object property by using JSONATA transform function but the array object is not getting removed. If pmtDtls.lnAprvlStatus ='D', the object in the pmtDtls should be removed.

Input data is

{
    "compCode": "0002",
    "pmtDtls": [
        {
         "partnerType": "V",
        "partnerId": "0001",
        "partnerName": "Partner 1",
        "payAmt": 1000,
        "lnAprvlStatus": "A",
        "payDesc": "nkjfkjfk nknfknk jkjkj"
    },
    {
        "partnerType": "V",
        "partnerId": "0005",
        "partnerName": "Partner 2",
        "payAmt": 2000,
        "lnAprvlStatus": "D",
        "payDesc": "fkjfk jfkfjkf jfkfjf"
    }
]

}

JSONATA expression being used is $ ~> |{}| {}, pmtDtls[lnAprvlStatus='D'] |

I am expecting an output after removal of pmtDtls.lnAprvlStatus = 'D' as

{
  "compCode": "0002",
  "pmtDtls": [
    {
      "partnerType": "V",
      "partnerId": "0001",
      "partnerName": "Partner 1",
      "payAmt": 1000,
      "lnAprvlStatus": "A",
      "payDesc": "nkjfkjfk nknfknk jkjkj"
    }
  ]
}

but the same object as input is being returned

3 Answers 3

0

$merge([$, { "pmtDtls": $.pmtDtls[lnAprvlStatus != "D"] }])

or

$ ~> | pmtDtls | {}, lnAprvlStatus = "D" ? $keys() |

should help you get the result you need.

2
  • Thanks for the help. Both the options gives correct result. However second option creates a blank object also in the result, which may be due to filtered sub Object not being part of the result. Thanks again
    – ashok jha
    Commented Jun 26 at 11:22
  • No problem. Yes, the second expression does leave blank objects which need filtering out with another expression.
    – Vlad Dimov
    Commented Jun 26 at 12:32
0

Here's how you can modify your expression to achieve this:

{
  "compCode": compCode,
  "pmtDtls": pmtDtls[lnAprvlStatus != "D"]
}

This expression will transform the input data by removing the pmtDtls object where lnAprvlStatus is "D", resulting in the expected output.

  • compCode retains the compCode property from the input.
  • pmtDtls[lnAprvlStatus != "D"] filters the pmtDtls array to exclude objects where lnAprvlStatus equals "D".
3
  • Thanks for your answer. It works !!! However my problem is - there are many more properties in the real object at the root level, which I removed in the question for easy understanding. To make it work as suggested by you, I need to write down all such properties in the jsonata expression. Is there any other way which can do it more efficiently?
    – ashok jha
    Commented Jun 26 at 3:25
  • { "compCode": compCode, "pmtDtls": pmtDtls[lnAprvlStatus != "D"], **@$** } **@$** is a wildcard that includes all other properties from the original object without explicitly specifying them. Commented Jun 26 at 4:54
  • This did not produce the result. I tried various other combinations of this path traversal without any success.
    – ashok jha
    Commented Jun 26 at 11:12
0

Just had something similar. In my opinion this is the most intuitive solution:

$ ~> | $ | {
    "pmtDtls": $.pmtDtls[lnAprvlStatus != "D"]
} |

This will not return empty objects and will only removearray members from pmtDtls - all other keys that may exist won't be removed.

Not the answer you're looking for? Browse other questions tagged or ask your own question.