0

I have a method that gets two Json nodes that should be Json array nodes of different sizes. I need to check if ArrayA contains all the elements of ArrayB. How do I do that?

private boolean isContains (JsonNode ArrayA, JsonNode ArrayB) {

    if (ArrayA.isArray() && ArrayB.isArray()) {
        // Here goes the missing code.
    }
}
3
  • By documentation equals should work in depth, see jackson.codehaus.org/1.7.9/javadoc/org/codehaus/jackson/… Commented Nov 9, 2014 at 20:17
  • They are not equal. In Java, we have a method list.contains(anotherList). Is there something similar in Jackson? Commented Nov 9, 2014 at 20:25
  • I misunderstood your question. The other answers are appropriate. Commented Nov 9, 2014 at 21:08

4 Answers 4

2

The JSON Patch reverse part might do what you are looking for.

See: https://github.com/fge/json-patch#json-diff-factorization

Edit: It is as easy as:

JsonNode diff = JsonDiff.asJson(first, second);

And than you can check whether diff contains removes or adds or both.

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

Comments

0

There seems to be no inbuilt method I can see, but JsonNode implements Iteratable so you can loop through both and do a compare. Something along the lines of:

boolean matches = true;

for (JsonNode nodeB : arrayB)
{
   boolean matchesInternal = false;

   for (JsonNode nodeA : arrayA)
   {
      if (nodeA.equals(nodeB))
      {
          matchesInternal = true;
          break;
      }
   }

   if (!matchesInternal) {
      matches = false;
      break;
   }
}

return matches;

4 Comments

could it be I have no matches() method?
I used nodeA.**asText()**.equals(nodeB.**asText()**) to compare the nodes as strings and it worked just fine. Thanks a lot.
@Alex if you check the asText() documentation, you will see it will return empty strings for compound elements, so that is a not too good solution, different things will be considered equals.
@Alex Sorry, I mistyped that. equals would be correct. I don't know that you have to call asText().
0

You can use zjsonpatch library, which presents the diff information in accordance with RFC 6902 (JSON Patch). Its very easy to use. Please visit its description page for its usage. This library is better than fge-json-patch (which was mentioned in above answer) because it can detect correctly items being inserted/removed from arrays.

Comments

0

For those who faced the same problem but prefer more flat code. You can use the Apache Commons's IterableUtils.toList() to convert the ArrayNode#elements() to List and then perform List#containsAll() operation to check whether they contain the same elements. It will look like this:

sourceArray.size() == targetArray.size() && IteratorUtils.toList(sourceArray.elements()).containsAll(IteratorUtils.toList(targetArray.elements()))

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.