I have some json and it's fairly complex -- (a bit too complex and open-ended to model using something like gson), and I need to extract string values from certain nodes into a list of strings.
The following code works, but due to the way my json works -- it's grabbing lots of extra stuff that I don't want (note: I don't own the json schema)
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);
List<JsonNode> keys = node.findValues("key") ;
for(JsonNode key: keys){
System.out.println(key.toString());
}
The contents of Json is fairly complex (Jira filter export) which looks like this:
{
"issues": [
{
"key":"MIN-123",
...
"fields":{
"key":"A_Elric"
}
}
]
}
Assertions:
I always want to extract issues[x].key and not any of the subkeys. I would prefer to extract this into a list, but any normal data structure is fine. I'm already using Jackson -- but gson is also an option if there's a sane way of doing so.
Thanks for the assist!
JsonPath is xpath for json, and it has a Java implementation.
Here is a working example to get issue keys without subkeys:
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class JsonPathTest {
public static String ROOT_ARRAY = "issues";
public static String KEY = "key";
// get all KEYs right under ROOT array
public static String jsonPath = String.format("$.%s[*].%s", ROOT_ARRAY, KEY);
public static void main(String[] args) {
try {
String jsonStr = new String(Files.readAllBytes(Paths.get("c:/temp/xx.json")));
Object jsonObj = Configuration.defaultConfiguration().jsonProvider().parse(jsonStr);
List<String> keys = JsonPath.parse(jsonObj).read(jsonPath);
System.out.println(keys);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class ExportFilter{
private static final String KEY = "key";
private List<Map<String,Object>> issues = new ArrayList<>();
//getters and setters
#JsonIgnore
public List<String> getKeys(){
return issues.stream()
.map(issue-> issue.get(KEY))
.filter(Objects::nonNull)
.map(Objects::toString)
.collect(toList());
}
}
Example usage:
ObjectMapper objectMapper = new ObjectMapper();
List<String> keys = objectMapper.readValue( .., ExportFilter.class).getKeys();
Related
I have a simple class in java like below:
class Simple {
private String name;
private String email;
}
I want to have behaviour of java.util.List<Simple> and Simple both according to input data that my program receives.
i.e.
Case 1::
if my program receives below kind of json-array input
{"simple" : [ {"name":"a", "email" : "a#z.com"}, {"name":"b", "email" : "b#z.com"} ]}
I need to parse it using List<Simple>
Case 2::
if my program receives below kind of json-object input
{"simple" : {"name":"c", "email" : "c#z.com"} }
I need to parse it using Simple
Note: I have tried using JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, but the problem is it is basically converting single value also into json-array at the time of writing json.
I need to persist json as it is, is there any other way to achieve this?
To avoid any Jackson customisation I would create wrapper class with an Object simple property. We can add two extra checking methods and two extra casting methods. It will allow Jackson to do it's logic and in runtime we can check what actually we have:
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Collections;
import java.util.List;
public class DateApp {
private final static JsonMapper JSON_MAPPER = JsonMapper.builder().enable(SerializationFeature.INDENT_OUTPUT).build();
public static void main(String[] args) throws Exception {
Simple object = new Simple("John", "john#doe.com");
SimpleWrapper wrapper = new SimpleWrapper();
wrapper.setSimple(object);
serializeAndDeserialize(wrapper);
wrapper.setSimple(Collections.singletonList(object));
serializeAndDeserialize(wrapper);
}
private static void serializeAndDeserialize(SimpleWrapper wrapper) throws JsonProcessingException {
String json = JSON_MAPPER.writeValueAsString(wrapper);
System.out.println("JSON:");
System.out.println(json);
wrapper = JSON_MAPPER.readValue(json, SimpleWrapper.class);
System.out.println("Wrapper:");
System.out.println(wrapper);
}
}
#Data
class SimpleWrapper {
private Object simple;
#JsonIgnore
public boolean isSimpleObject() {
return simple instanceof Simple;
}
#JsonIgnore
public boolean isSimpleList() {
return simple instanceof List;
}
#JsonIgnore
public Simple getSimpleAsObject() {
return (Simple) simple;
}
#JsonIgnore
public List<Simple> getSimpleAsList() {
return (List<Simple>) simple;
}
}
#Data
#NoArgsConstructor
#AllArgsConstructor
class Simple {
private String name;
private String email;
}
Above code prints:
JSON:
{
"simple" : {
"name" : "John",
"email" : "john#doe.com"
}
}
Wrapper:
SimpleWrapper(simple={name=John, email=john#doe.com})
JSON:
{
"simple" : [ {
"name" : "John",
"email" : "john#doe.com"
} ]
}
Wrapper:
SimpleWrapper(simple=[{name=John, email=john#doe.com}])
You can use JsonNode.isArray() (or JsonNode.isObject()) to perform this check.
Then you can parse the node into a list with ObjectReader.readValue() or into a POJO using ObjectMapper.treeToValue().
String myJson = """
{"simple" : {"name":"c", "email" : "c#z.com"} }
""";
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(myJson);
if (node.isArray()) {
ObjectReader reader = mapper.readerFor(new TypeReference<List<Simple>>() {});
List<Simple> list = reader.readValue(node);
// do something with a list
} else {
Simple pojo = mapper.treeToValue(node, Simple.class);
// do something else with a single object
}
Jackson is able to parse any json into a map where value is any object. you can then inquire on the type of the map value
Map<String, Object> map = new ObjectMapper().readValue(jsonInput, Map.class);
Object value = map.get("simple");
if (value instanceof Collection) { // will return false for null
Collection<Simple> simples = (Collection<Simple>)value;
}
else if (value instanceof Simple) {
Simple simple = (Simple)value;
}
else {
System.err.println("unrecognized");
}
You only need to read the first node, simple and check if it is an array - using isArray() method.
public class Main{
public static void main(String args[]) {
//String inputString = [your input];
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(inputString);
JsonNode simpleNode = root.findPath("simple");
if(simpleNode.isArray()) {
//you have an array
} else {
// you have an element
}
}
}
I use Jackson to serialize/deserialize JSON.
I have a List<String> in which all elements inside are already serialized in JSON format. I would like to generate a big JSON from that List.
In other word, I have:
List<String> a = new ArrayList<>();
a[0] = JSON_0
a[1] = JSON_1
...
a[N] = JSON_N
And I would like to render:
[
{JSON_0},
{JSON_1},
...
{JSON_N}
]
What is the best way to do so using Jackson?
Probably the simpler solution would be to create ArrayNode and use addRawValue method:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.util.RawValue;
public class JsonApp {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
ArrayNode nodes = mapper.getNodeFactory().arrayNode();
nodes.addRawValue(new RawValue("{}"));
nodes.addRawValue(new RawValue("true"));
nodes.addRawValue(new RawValue("{\"id\":1}"));
System.out.println(mapper.writeValueAsString(nodes));
}
}
Above code prints:
[{},true,{"id":1}]
You can also, create a POJO with list and use #JsonRawValue annotation. But if you can not have extra root object you need to implement custom serialiser for it. Example with POJO and custom serialiser:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JsonApp {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
List<String> jsons = new ArrayList<>();
jsons.add("{}");
jsons.add("true");
jsons.add("{\"id\":1}");
RawJsons root = new RawJsons();
root.setJsons(jsons);
System.out.println(mapper.writeValueAsString(root));
}
}
#JsonSerialize(using = RawJsonSerializer.class)
class RawJsons {
private List<String> jsons;
public List<String> getJsons() {
return jsons;
}
public void setJsons(List<String> jsons) {
this.jsons = jsons;
}
}
class RawJsonSerializer extends JsonSerializer<RawJsons> {
#Override
public void serialize(RawJsons value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartArray();
if (value != null && value.getJsons() != null) {
for (String json : value.getJsons()) {
gen.writeRawValue(json);
}
}
gen.writeEndArray();
}
}
If you need to have SerializationFeature.INDENT_OUTPUT feature enabled for all items in array, you need to deserialise all inner objects and serialise them again.
See also:
How can I include raw JSON in an object using Jackson?
Handling raw JSON values using Jackson
the simple fact of having the character '[' we are marking that it is an array so what I recommend to put the list into a JSON array.
I would need a little more information to help you, since it doesn't make much sense to use a JSON String, since a JSON is composed of Key / Value, it is best to make a bean / object with the attribute.
Example:
class Object {
private String attribute = value;
}
{attribute: value}
I use reasteasy to call REST API.
My problem is that the JSON from the REST API has dynamic values. For example:
"labels": {
"kubernetes.io/hostname": "192.168.200.176",
"node": "master"
}
Where "node" and "kubernetes.io/hostname" could be any string.
I've tried to map "labels" into a Map <String, String> object. The variable is correctly created but it remains empty. How can I generate a dictionary with for example {"kubernetes.io/hostname": "192.168.200.176", "node": "master"}?`
If you're fine with just creating a Map instead of a specific domain object, you can simply parse the JSON yourself to get a list of the keys and create the Map yourself.
Here's an example using org.json:
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
public class Scratch {
public static void main(String[] args) throws Exception {
String json = "{ \"labels\": { \"kubernetes.io/hostname\": \"192.168.200.176\", \"node\": \"master\" } }";
Map<String, String> library = new HashMap<>();
// parse the input string
JSONObject labels = new JSONObject(json).getJSONObject("labels");
// iterate over keys and insert into Map
for (String key : labels.keySet()) {
library.put(key, labels.getString(key));
}
System.out.println(library);
// {kubernetes.io/hostname=192.168.200.176, node=master}
}
}
You can also do more-or-less the same thing with Gson, by simply wrapping the Map in a container class, and letting it do the actual deserialization:
import java.util.Map;
import com.google.gson.Gson;
public class Scratch {
public static void main(String[] args) throws Exception {
String json = "{ \"labels\": { \"kubernetes.io/hostname\": \"192.168.200.176\", \"node\": \"master\" } }";
Library library = new Gson().fromJson(json, Library.class);
System.out.println(library.labels);
// {kubernetes.io/hostname=192.168.200.176, node=master}
}
}
class Library {
Map<String, String> labels;
}
In both cases, notice that you have to get the data from inside the "labels" field of the JSON, not the top level.
I'm trying to serialize an object in JSON using a JSP like format using the following code:
ArrayList<AccountBean> al = new ArrayList<AccountBean>();
al = vc.getAccountName();
int i=0;
out.print("[");
while(i<al.size()){
Gson gson = new GsonBuilder().setPrettyPrinting().create();
out.print("{ID"+al.get(i).getAno()+":name"+al.get(i).getAccount_name()+"},");
i++;
}
out.print("]");
I'm getting a output like this:
[{ID1:nameEquity Share Capitals},{ID2:nameCalls In Arear},]
but my requirement is something like this:
[{"ID1":"nameEquity Share Capitals"},{"ID2":"nameCalls In Arear"}]
out.print('{"ID'+al.get(i).getAno()+'":"name'+al.get(i).getAccount_name()+'"},')
use ' to open/close the string, and " to wrap your json keys/values.
Otherwise you can do like this
out.print("{\"ID"+al.get(i).getAno()+"\":\"name"+al.get(i).getAccount_name()+"\"},")
escaping the quotes with \"
Anyway, have you tried this?
String json = gson.toJson(al)
Have a look here for more info: https://sites.google.com/site/gson/gson-user-guide
Best way to do this is using a custom serializer and I can edit this answer posting one if you want to go deeper.
However, since you are quite new to JSON and Gson I would answer with this simple code that you can paste&try in you IDE. I just "convert" you bean into a map, and the use Gson to serialize.
package stackoverflow.questions;
import java.util.*;
import com.google.gson.Gson;
public class Q20323412 {
public static class AccountBean{
Integer _id;
String _name;
public String getAccount_name(){
return _name;
}
public Integer getAno(){
// what a weird name, in italian for this method..
return _id;
}
public AccountBean(Integer id, String name){
_id = id;
_name = name;
}
}
/**
* #param args
*/
public static void main(String[] args) {
ArrayList<AccountBean> al = new ArrayList<AccountBean>();
al.add(new AccountBean(1, "Equity Share Capitals"));
al.add(new AccountBean(2, "Calls In Arear"));
ArrayList<Map> al2 = new ArrayList<>();
for(AccountBean account : al){
HashMap hm = new HashMap();
hm.put("ID"+ account.getAno(), "name"+account.getAccount_name());
al2.add(hm);
}
Gson g = new Gson();
System.out.println(g.toJson(al2));
}
}
Since you did not post your bean, I invented one that has features similar to your's.
Would it be possible if someone could help me parse this json result. I have retrieved the result as a string
{"query":{"latitude":39.9889,"longitude":-82.8118},"timestamp":1310252291.861,"address":{"geometry":{"coordinates":[-82.81168367358264,39.9887910986731],"type":"Point"},"properties":{"address":"284 Macdougal Ln","distance":"0.02","postcode":"43004","city":"Columbus","county":"Franklin","province":"OH","country":"US"},"type":"Feature"}}
Jackson. Simple and intuitive to use. For me the best available. Start out with Simple Data Binding, it will throw everything it finds in Maps and Lists.
Like this:
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> yourData = mapper.readValue(new File("yourdata.json"), Map.class);
That's all that's needed.
A good and quick introduction can be found here
And a full working example with your actual data:
import java.io.IOException;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
public class Main {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
Map<?,?> rootAsMap = mapper.readValue(
"{\"query\":{\"latitude\":39.9889,\"longitude\":-82.8118},\"timestamp\":1310252291.861,\"address\":{\"geometry\":{\"coordinates\":[-82.81168367358264,39.9887910986731],\"type\":\"Point\"},\"properties\":{\"address\":\"284 Macdougal Ln\",\"distance\":\"0.02\",\"postcode\":\"43004\",\"city\":\"Columbus\",\"county\":\"Franklin\",\"province\":\"OH\",\"country\":\"US\"},\"type\":\"Feature\"}}".getBytes(),
Map.class);
System.out.println(rootAsMap);
Map query = (Map) rootAsMap.get("query");
Map address = (Map) rootAsMap.get("address");
Map addressProperties = (Map) address.get("properties");
String county = (String) addressProperties.get("county");
System.out.println("County is " + county);
}
}
Now, this whole Map juggling also illustrates Bozho's point pretty well, using full binding (by creating a Java class that reflects the content of the JSON data) will work better in the end.
The two best options that I know of are:
Jackson
gson
Using them is a matter of calling one method of the mapper. But remember that since Java is statically-typed, you may have to create an object that has the required structure. (You don't have to, but it feels more natural)
From http://www.json.org, under the Java section:
http://www.json.org/java/index.html
http://json-lib.sourceforge.net/
http://code.google.com/p/json-simple/
http://code.google.com/p/jjson/
Pick your poison
With Jackson, following is the approach I'd take. Since the coordinates in the JSON come in two different formats -- sometimes an object, sometimes an array -- the solution is mildly complicated with necessary custom deserialization processing.
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.ObjectCodec;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.module.SimpleModule;
import org.codehaus.jackson.node.ArrayNode;
public class JacksonFoo
{
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY));
mapper.registerModule(
new SimpleModule("CoordinatesDeserializer", Version.unknownVersion())
.addDeserializer(Coordinates.class, new CoordinatesDeserializer()));
Result result = mapper.readValue(new File("input.json"), Result.class);
System.out.println(mapper.writeValueAsString(result));
}
}
class CoordinatesDeserializer extends JsonDeserializer<Coordinates>
{
#Override
public Coordinates deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException
{
ObjectCodec codec = jp.getCodec();
JsonNode node = codec.readTree(jp);
if (node.isObject())
{
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY));
return mapper.readValue(node, Coordinates.class);
}
// else it's an array
ArrayNode array = (ArrayNode) node;
Coordinates coordinates = new Coordinates();
coordinates.latitude = codec.treeToValue(array.get(0), BigDecimal.class);
coordinates.latitude = codec.treeToValue(array.get(1), BigDecimal.class);
return coordinates;
}
}
class Result
{
Coordinates query;
BigDecimal timestamp;
Address address;
}
class Coordinates
{
BigDecimal latitude;
BigDecimal longitude;
}
class Address
{
String type;
Geometry geometry;
AddressDetails properties;
}
class Geometry
{
String type;
Coordinates coordinates;
}
class AddressDetails
{
String address;
BigDecimal distance;
String postcode;
String city;
String county;
String province;
String country;
}