Skip to content

Instantly share code, notes, and snippets.

@glowinthedark
Created June 17, 2024 13:04
Show Gist options
  • Save glowinthedark/969277020b7793fca9fd9fe5141a900f to your computer and use it in GitHub Desktop.
Save glowinthedark/969277020b7793fca9fd9fe5141a900f to your computer and use it in GitHub Desktop.
JsonPointer example
package com.legbehindneck;
import java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonPointer;
public class Main {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String json = "{\"name\":{\"first\":\"John\",\"last\":\"Doe\"},\"age\":30,\"city\":\"New York\"}";
JsonNode node = null;
try {
node = mapper.readTree(json);
JsonPointer pointer = JsonPointer.compile("/name/first");
JsonNode firstNameNode = node.at(pointer);
System.out.println("First Name: " + firstNameNode.asText());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment