Jackson deserialize json string with "pseudo array" of objects - java

I'm trying to read data from this API https://coinmarketcap.com/api/
For this endpoint https://api.coinmarketcap.com/v2/ticker/.
I'm having issues mapping the data field to a POJO. The field really contains an array of objects but in terms of the json it's not really defined as an array.
i.e. instead of
data: [{"id":"1","name":"some object"},{"id":"5","name":"another object"},...]
the json has named fields like so
data: {"1":{"id":"1","name":"some object"},"5":{"id":"5","name":"another object"},...}
I can manually parse this using
objectMapper.readTree(new URL("https://api.coinmarketcap.com/v2/ticker/"));
but is there a way automatically map these to a List?

You can parse it into a map (as #teppic said) and then get the map values as a list.
To deserialize into a map, you can see the answer from this question: Deserializing into a HashMap of custom objects with jackson
TypeFactory typeFactory = mapper.getTypeFactory();
MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, Theme.class);
HashMap<String, Theme> map = mapper.readValue(json, mapType);
Assuming you have a class called Item with the id and name fields, you can do this:
String json = "{\"1\":{\"id\":\"1\",\"name\":\"some object\"},\"5\":{\"id\":\"2\",\"name\":\"another object\"}}";
ObjectMapper mapper = new ObjectMapper();
// create your map type <String, Item>
TypeFactory typeFactory = mapper.getTypeFactory();
MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, Item.class);
HashMap<String, Item> map = mapper.readValue(json, mapType);
// get the list
List<Item> list = new ArrayList<Item>(map.values());
System.out.println(list);
Output:
[Item [id=1, name=some object], Item [id=2, name=another object]]
Your other option would be a custom deserializer, or reading the tree as you mentioned.

try this
String json = "[{\"name\":\"Steve\",\"lastname\":\"Jobs\"}]";
JsonArray jArray = (JsonArray)new JsonParser().parse(json);
String sName = jArray.get(0).getAsJsonObject().get("name").getAsString());
String sLastName = jArray.get(0).getAsJsonObject().get("lastname").getAsString());
see you later.

Related

Jackson: understand if source JSON is an array or an object

Parsing JSON in Jackson library would require:
for an object
MapType hashMapType = typeFactory.constructMapType(HashMap.class, String.class, Object.class);
Map<String, Object> receivedMessageObject = objectMapper.readValue(messageBody, hashMapType);
for an array of objects
Map[] receivedMessage = objectMapper.readValue(messageBody, HashMap[].class)
What would be the best way to check whether I have array or object in messageBody, in order to route to the correct parsing? Is it just to directly check for array token in MessageBody?
An option is just to treat everything that might be an array as an array. This is often most convenient if your source JSON has just been auto-transformed from XML or has been created using an XML-first library like Jettison.
It's a sufficiently common use case that there's a Jackson switch for this:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
You can then just deserialize a property into a collection type, regardless of whether it's an array or an object in the source JSON.
If you want to know whether your input is an array or an object, you can simply use the readTree method. A simple example:
ObjectMapper mapper = new ObjectMapper();
String json1 = "{\"key\": \"value\"}";
String json2 = "[\"key1\", \"key2\"]";
JsonNode tree1 = mapper.readTree(json1);
System.out.println(tree1.isArray());
System.out.println(tree1.isObject());
JsonNode tree2 = mapper.readTree(json2);
System.out.println(tree2.isArray());
System.out.println(tree2.isObject());
If you want to be able to deserialize to multiple types, have a look at Polymorphic Deserialization
This is what I did based on the answer from #ryanp :
public class JsonDataHandler {
public List<MyBeanClass> createJsonObjectList(String jsonString) throws JsonMappingException, JsonProcessingException {
ObjectMapper objMapper = new ObjectMapper();
objMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
List<MyBeanClass> jsonObjectList = objMapper.readValue(jsonString, new TypeReference<List<MyBeanClass>>(){});
return jsonObjectList;
}
}

Convert List<MyDTO> to List<Map<String,String>> using Gson

I have DTO as below :
public class MyDTO {
private String MEValueName;
private String MyId;
// getters and setters
}
Gson gson = new Gson();
List<MyDTO> list = new ArrayList<MyDTO>();
MyDTO dto = new MyDTO();
dto.setMEValueName("raghu");
dto.setMyId("qwer");
MyDTO dto1 = new MyDTO();
dto1.setMEValueName("raghuveer");
dto1.setMyId("qwer1");
list.add(dto);
list.add(dto1);
String json = gson.toJson(list);
System.out.println(json);
// below line is failing
List<Map<String, String>> list1 = gson.fromJson(json, new TypeToken<List<Map<String, String>>>(){}.getType());
System.out.println(list1);
when i run this i get the follow error :
java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 11 path $[0].
Also i want to use a generic type so that i can use other DTOs also to convert to list of map. Kindly suggest.
I see two immediate problems...
The json-string is of an object of type List<MyDTO>.
gson.fromJson must have a TypeToken of that same object type.
And even if the fromJson worked, you cant assign List<MyDTO> to a list of type List<Map<String,String>>.
Edit (doing this by memory so it might need some fine tuning to compile):
List<Map<String, String>> list1 = new List();
Map<String,String> map = new HashMap<>();
list1.add(map);
map.add("some key",json);

getting parameter value of json object in java

I have a JsonObject :
{"result":[{"active":"true"}]}
How can I get value of active?
You can use Jackson
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> parsedMap = mapper.readValue(jsonString, new TypeReference<HashMap<String, Object>>() {
});
We get a Map with key value pairs. With your input you will get a Map<List<Map<String,String>>>. So to get "active", you can use something like parsedMap.get("result").get(0).get("active") (Note: This is just a pseudo code)

Get JSON key name using GSON

I have a JSON array which contains objects such as this:
{
"bjones": {
"fname": "Betty",
"lname": "Jones",
"password": "ababab",
"level": "manager"
}
}
my User class has a username which would require the JSON object's key to be used. How would I get the key of my JSON object?
What I have now is getting everything and creating a new User object, but leaving the username null. Which is understandable because my JSON object does not contain a key/value pair for "username":"value".
Gson gson = new Gson();
JsonParser p = new JsonParser();
JsonReader file = new JsonReader(new FileReader(this.filename));
JsonObject result = p.parse(file).getAsJsonObject().getAsJsonObject("bjones");
User newUser = gson.fromJson(result, User.class);
// newUser.username = null
// newUser.fname = "Betty"
// newUser.lname = "Jones"
// newUser.password = "ababab"
// newUser.level = "manager"
edit:
I'm trying to insert "bjones" into newUser.username with Gson, sorry for the lack of clarification
Use entrySet to get the keys. Loop through the entries and create a User for every key.
JsonObject result = p.parse(file).getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entrySet = result.entrySet();
for(Map.Entry<String, JsonElement> entry : entrySet) {
User newUser = gson.fromJson(p.getAsJsonObject(entry.getKey()), User.class);
newUser.username = entry.getKey();
//code...
}
Using keySet() directly excludes the necessity in iteration:
ArrayList<String> objectKeys =
new ArrayList<String>(
myJsonObject.keySet());
Your JSON is fairly simple, so even the manual sort of methods (like creating maps of strings etc for type) will work fine.
For complex JSONs(where there are many nested complex objects and lists of other complex objects inside your JSON), you can create POJO for your JSON with some tool like http://www.jsonschema2pojo.org/
And then just :
final Gson gson = new Gson();
final MyJsonModel obj = gson.fromJson(response, MyJsonModel.class);
// Just access your stuff in object. Example
System.out.println(obj.getResponse().getResults().get(0).getId());

Jackson - unwrapping object

I am getting responses from server that are wrapped with some additional info. For example:
{
"response_a" : ...,
"some_metadata" : 1234,
"more_metadata" : abcd
}
or
{
"response_b" : [...],
"some_metadata" : 1234,
"more_metadata" : abcd
}
The "response_x" can be custom object, list or hashmap, it can have different name depending on request.
Is there a way deserialize just the response_x, or get it as string using jackson?
You can deserialize above JSON to Map and retrieve the property using get method:
ObjectMapper mapper = new ObjectMapper();
MapType mapType = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);
Map<String, Object> result = mapper.readValue(json, mapType);
Object responseX = result.get("response_x");

Categories