0

I am exploring JSONAta right now for fun and have a question on how to solve something.

I would like to remove objects from an array that exist in another array based on a match of the entire object (or if that's not possible, match on the Code attribute). For example:

$largerArray := [{"Name": "A", "Code": "100"} ,{"Name": "B", "Code": "200"},{"Name": "C", "Code": "300"}, {"Name": "D", "Code": "400"}];
$objectsToExclude := [{"Name": "A", "Code": "100"} ,{"Name": "B", "Code": "200"},{"Name": "C", "Code": "300"}];
$expectedArray := [{"Name": "D", "Code": "400"}];

So you can see only D is left in the array since A, B, C exist in both arrays.

Is there a way clean way to do this in JSONAta?

I was able to do simple exclusions such as the following:

$sourceArray := [1,2,3,4,5];
$removeArray := [3,4,5];

$result := $sourceArray[$not($ in $removeArray)];

My attempts at doing this with more complex objects is failing.

2 Answers 2

0

Given a JSON input such as:

[
  {
    "Name": "A",
    "Code": "100"
  },
  {
    "Name": "B",
    "Code": "200"
  },
  {
    "Name": "C",
    "Code": "300"
  },
  {
    "Name": "D",
    "Code": "400"
  }
]

To exclude objects based on the Code attribute, you can try:

(
  $objectsToExclude := [
    { "Name": "A", "Code": "100" },
    { "Name": "B", "Code": "200" },
    { "Name": "C", "Code": "300" }
  ];

  $expectedArray := $[$not(Code in $objectsToExclude.Code)];
)

Playground link: https://jsonatastudio.com/playground/2819d9c1

Comparing objects strictly (attribute by attribute) is possible but it won't be pretty or efficient. I'll have another think if you'd prefer that instead.

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

4 Comments

Thank you for your response! Can you take a look at the playground link here and see why the dynamic exclusion is not working? jsonatastudio.com/playground/2819d9c1
I am not sure what you mean. Please can you share your code here or in a different link?
Apologies - please see it here: stedi.link/GtgeJEt
Thanks for the link. I had a look and believe you are missing $$ in $[$not(itemCode in $$.item.removeSubItems.subItems.itemCode)] to reference the source JSON input. I've posted a separate answer.
0

Given a JSON input such as:

{
  "item": {
    "removeSubItems": [
      {
        "subItems": [
          {
            "name": "A",
            "itemCode": "100"
          },
          {
            "name": "B",
            "itemCode": "200"
          }
        ]
      }
    ]
  },
  "selectedSubItems": [
    {
      "subItems": [
        {
          "name": "A",
          "itemCode": "100"
        },
        {
          "name": "B",
          "itemCode": "200"
        }
      ]
    },
    {
      "subItems": [
        {
          "name": "C",
          "itemCode": "300"
        }
      ]
    }
  ]
}

And a JSONata expression like:

$comboItems := {
  "ItemsToExclude": item.removeSubItems.subItems.itemCode,
  "ItemsWithExclusionsManualIsWorking": selectedSubItems.subItems.$[$not(itemCode in ["100", "200"])].{ "itemCode": itemCode }[],
  "ItemsWithExclusionsDynamicIsNotWorking": selectedSubItems
    .subItems
    .$[$not(itemCode in $$.item.removeSubItems.subItems.itemCode)]
    .{ "itemCode": itemCode }[]
}

The output should be:

{
  "ItemsToExclude": [
    "100",
    "200"
  ],
  "ItemsWithExclusionsManualIsWorking": [
    {
      "itemCode": "300"
    }
  ],
  "ItemsWithExclusionsDynamicIsNotWorking": [
    {
      "itemCode": "300"
    }
  ]
}

Playground link: https://jsonatastudio.com/playground/c469b093

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.