2

My JSON array file:

[
    {
      "setName": "set-1",
      "testTagName": "Test1",
      "methodName": "addCustomer"
    },
    {
        "setName": "set-1",
        "testTagName": "Test2",
        "methodName": "addAccount"
    },
    {
        "setName": "set-2",
        "testTagName": "Test3",
        "methodName": "addRole"
    }
  ]

I use Java. I have the above JSON Array in a Gson object. How do I iterate through this Gson array to check if a particular method name (eg: addRole), exists in the array for the key "methodName" in any of the objects of the JSON array? I am expecting true/false as a result.

I checked the GSON doc - (https://github.com/google/gson/blob/master/gson/src/main/java/com/google/gson/JsonObject.java#L141)

The has method seems to check for the key. I am looking for a method that can iterate through the objects of the array and check if a particular value exists for a specific key.

How can I achieve this?

3
  • 1
    what type of container holds these JSON objects? typically they'll be held in an array and you just iterate over the array and use those methods you sourced on each object like jsonArray[index].get("methodName").equals("addRole") Commented Jul 15, 2021 at 22:59
  • It's stored in a .json file. In the code, it is in the form of a GSON object. Commented Jul 15, 2021 at 23:00
  • Do we take it you want a gson example? Commented Jul 15, 2021 at 23:53

2 Answers 2

5

First you need to deserialize the JSON code to a JsonArray in this way:

JsonArray jsonArr = gson.fromJson(jsonString, JsonArray.class);

After that you can create this method:

public boolean hasValue(JsonArray json, String key, String value) {
    for(int i = 0; i < json.size(); i++) {  // iterate through the JsonArray
        // first I get the 'i' JsonElement as a JsonObject, then I get the key as a string and I compare it with the value
        if(json.get(i).getAsJsonObject().get(key).getAsString().equals(value)) return true;
    }
    return false;
}

Now you can call the method:

hasValue(jsonArr, "methodName", "addRole");
Sign up to request clarification or add additional context in comments.

2 Comments

You can do this with regexp without deserializion "KEY"\s*:\s*"VALUE"
@GeorgeVassilev Do you realize how bad your suggested approach is?
3

You can get the JSON in a JsonArray and then iterate over the elements while checking for the desired value.

One approach is suggested by @Crih.exe above. If you want to use Streams, you can convert the JsonArray into a stream and use anyMatch to return a boolean value

...
// Stream JsonArray
Stream<JsonElement> stream = StreamSupport.stream(array.spliterator(), true);

// Check for any matching element
boolean result = stream.anyMatch(e -> e.getAsJsonObject()
                   .get("methodName").getAsString().equals("addRole"));

System.out.println(result);
...

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.