Merge pull request #14553 from sam-gardner/BAEL-6818-check-value-exists-json-array

BAEL-6818 Add examples for checking if a value exists in a JSON array
This commit is contained in:
davidmartinezbarua 2023-08-22 14:58:27 -03:00 committed by GitHub
commit b4131e330a
3 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>json-arrays</artifactId>
<name>json-arrays</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>json-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>javax.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId>
<version>${jsonb-api.version}</version>
</dependency>
</dependencies>
<properties>
<jsonb-api.version>1.0</jsonb-api.version>
<json.version>20230227</json.version>
<gson.version>2.8.5</gson.version>
<javax.version>1.1.2</javax.version>
<json-unit-assertj.version>2.28.0</json-unit-assertj.version>
</properties>
</project>

View File

@ -0,0 +1,47 @@
package com.baeldung.checkforkey;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Objects;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
public class CheckForKeyUnitTest {
private final String exampleJson = "[{\"colour\":\"red\"},{\"colour\":\"blue\"},{\"colour\":\"green\"}]";
@Test
public void givenJsonArray_whenUsingJackson_thenDetectKeyInArray() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode tree = objectMapper.readTree(exampleJson);
Stream<JsonNode> s = StreamSupport.stream(tree.spliterator(), false);
boolean result = s.map(entry -> entry.get("colour"))
.filter(Objects::nonNull)
.anyMatch(colour -> "green".equals(colour.asText()));
assertTrue(result);
}
@Test
public void givenJsonArray_whenUsingGson_thenDetectKeyInArray() {
Gson gson = new Gson();
JsonArray parsed = gson.fromJson(exampleJson, JsonArray.class);
Stream<JsonElement> s = StreamSupport.stream(parsed.spliterator(), false);
boolean result = s.map(entry -> entry.getAsJsonObject()
.get("colour"))
.filter(Objects::nonNull)
.anyMatch(colour -> "green".equals(colour.getAsString()));
assertTrue(result);
}
}

View File

@ -16,6 +16,7 @@
<modules>
<module>json</module>
<module>json-2</module>
<module>json-arrays</module>
<module>json-conversion</module>
<module>json-path</module>
<module>gson</module>