BAEL-6818 Add examples for checking if a value exists in a JSON array

This commit is contained in:
Sam Gardner 2023-08-08 12:22:49 +01:00
parent d4fec3ad45
commit 960fe5312e
3 changed files with 93 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,45 @@
package com.baeldung.checkforkey;
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()));
assert (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()));
assert (result);
}
}

View File

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