0

I am using JSONata to get the entire input back in the result set, but with certain elements within an array specified inside the input removed.

This gets me to the "transform" operation documented here: https://docs.jsonata.org/other-operators#-------transform

where you can match data by a path, supply an object to add to whatever is matched, and then optionally specify a string or an array of strings to represent the keys for the data you want removed.

But can you actually use this operation to remove an element from an array that matches criterion? I can turn it into an empty object, but that's not what I want. I want to remove it altogether.

To use the Product-Order example seen in try.jsonata.org, consider this example: https://try.jsonata.org/QaZyN6nHr

The expression $ ~> |Account.Order.Product[SKU!="0406654608"]|$, $keys($)| is returning everything right - finding and replacing all elements in the Product element that does not have a particular SKU. The operand $keys($) above says "remove all keys from the selected object - an empty object. So we begin with this:

{
  "Account": {
    "Account Name": "Firefly",
    "Order": [
      {
        "OrderID": "order103",
        "Product": [
          {
            "Description": {
              "Colour": "Purple",
              "Depth": 210,
              "Height": 200,
              "Weight": 0.75,
              "Width": 300
            },
...
            "SKU": "0406654608"
          },
          {
      ...
            "SKU": "0406634348"
          }
        ]
      },
      {
        "OrderID": "order104",
        "Product": [
          { 
            ....
            "SKU": "xxxx",
          },
          ....
        ]
      },
      ....
    ]
  }
}

But per the above renders empty objects:

{
  "Account": {
    "Account Name": "Firefly",
    "Order": [
      {
        "OrderID": "order103",
        "Product": [
          {
            "Description": {
              "Colour": "Purple",
              "Depth": 210,
              "Height": 200,
              "Weight": 0.75,
              "Width": 300
            },
            "Price": 34.45,
            "Product Name": "Bowler Hat",
            "ProductID": 858383,
            "Quantity": 2,
            "SKU": "0406654608"
          },
          {}
        ]
      },
      {
        "OrderID": "order104",
        "Product": [
          {},
          {}
        ]
      }
    ]
  }
}

So the main problem is I don't just want to delete the values within the object that inside the array... I want to delete the object itself.

So how can I get it so the output looks like this?

{
  "Account": {
    "Account Name": "Firefly",
    "Order": [
      {
        "OrderID": "order103",
        "Product": [
          {
            "Description": {
              "Colour": "Purple",
              "Depth": 210,
              "Height": 200,
              "Weight": 0.75,
              "Width": 300
            },
            "Price": 34.45,
            "Product Name": "Bowler Hat",
            "ProductID": 858383,
            "Quantity": 2,
            "SKU": "0406654608"
          }
          /* NO EMPTY OBJECT */
        ]
      },
      {
        "OrderID": "order104",
        "Product": [
          /* NO EMPTY OBJECTS */
        ]
      }
    ]
  }
}

So what to I put in the 'delete' portion below?

$ ~> |Account.Order.Product[SKU!="0406654608"]|$, <BLAST THIS>)|`

1 Answer 1

0

Rather than try and delete individual products, just replace the Product array with one that contains the products you want:

$ ~> | Account.Order | { 'Product': [Product[SKU='0406654608']] } |

See https://try.jsonata.org/9GJeYZdd0

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.