0

In the following array how can the object with "step":"2" be removed from the sub-array "subGroupB" only when "code":"code-333"?

{
  "mainGroup":[
    {
      "subGroupA":{
         "id": "id-111",
         "code": "code-111"
      },
      "subGroupB": [
         {
         "step": "1",
         "code": "code-111"
         },
         {
         "step": "2",
         "code": "code-111"
         }    
       ]
    },
    {
      "subGroupA":{
         "id": "id-222",
         "code": "code-222"
      },
      "subGroupB": [
         {
         "step": "1",
         "code": "code-222"
         },
         {
         "step": "2",
         "code": "code-222"
         }   
       ]
    },
    {
      "subGroupA":{
         "id": "id-333",
         "code": "code-333"
      },
      "subGroupB": [
         {
         "step": "1",
         "code": "code-333"
         },
         {
         "step": "2",
         "code": "code-333"
         }}           
       ]
    }
  ]
}

The expected result is:

{
  "mainGroup":[
    {
      "subGroupA":{
         "id": "id-111",
         "code": "code-111"
      },
      "subGroupB": [
         {
         "step": "1",
         "code": "code-111"
         },
         {
         "step": "2",
         "code": "code-111"
         }    
       ]
    },
    {
      "subGroupA":{
         "id": "id-222",
         "code": "code-222"
      },
      "subGroupB": [
         {
         "step": "1",
         "code": "code-222"
         },
         {
         "step": "2",
         "code": "code-222"
         }   
       ]
    },
    {
      "subGroupA":{
         "id": "id-333",
         "code": "code-333"
      },
      "subGroupB": [
         {
         "step": "1",
         "code": "code-333"
         }           
       ]
    }
  ]
}

I have tried to $filter the last object's sub-array and $append it to the first two objects using variables, but the last object gets broken into two objects and I end up with 4 objects instead of three.

1 Answer 1

1

You can do it by filtering the subGroupB inside a mapped mainGroup like this:

{
  "mainGroup": mainGroup.{
    "subGroupA": subGroupA,
    "subGroupB": subGroupB[$not(code="code-333" and step="2")][]
  }[]
}

See it on the playground: https://stedi.link/QOLs3Mf

Alternatively, you can use the transform operator, if you don't want to reconstruct the whole object structure yourself:

$$ ~> |mainGroup|{ "subGroupB": subGroupB[$not(code="code-333" and step="2")][] }|

Also, on the playground: https://stedi.link/q5jEof2

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

2 Comments

These both work great! Question - in the first answer, using filtering, what does the last [] do?
This is to keep a single-item array as an array, since JSONata treats single-value arrays equal to that value itself: docs.jsonata.org/…

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.