Mule JSON data to objects - java

I have a json object
data = {
'ad': {
"date":"2013-06-05",
"catagory":"6",
"subcatagory":"5",
"text":"John john",
"ssn":"1306743999",
"email":"jonbrynjar#365.is",
"phone":"8612001"
},
'cc-info': {
"amount": "70",
"cardNumber": "4222222222222",
"expiryDate": "1215",
"currency": "ISK"
},
'dates': [
{ 'date': '2013-06-18', 'media': 1 },
{ 'date': '2013-06-19', 'media': 3 }
]
}
Then I have a subflow that takes the "cc-info" part of that json object and uses that data to call a third party service.
To extract the "cc-info" part of the json object I use #JsonAutoDetect class
#JsonAutoDetect
public class Handpoint {
private String amount;
private String cardNumber;
private String expiryDate;
private String currency;
public String getAmount() { return this.amount; }
public void setAmount(String amount) { this.amount = amount; }
public String getCardNumber() { return this.cardNumber; }
public void setCardNumber(String cardNumber) { this.cardNumber = cardNumber; }
public String getExpiryDate() { return this.expiryDate; }
public void setExpiryDate(String expireDate) { this.expiryDate = expireDate; }
public String getCurrency() { return this.currency; }
public void setCurrency(String currency) { this.currency = currency; }
}
When I send in the whole json object I get an error.
The question is: Do I have to put every variable in the json object into my #JsonAutoDetect class ?
Or what would be best practice for this.
I have verified that my code works when I just send in the "cc-info" part of the json objcet.

You don't need that #JsonAutoDetect, it doesn't do anything different from defaults without arguments.
But if your question is whether you can just ignore unknown properties, answer is yes. Here are couple of ways.
For example:
mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
would do the trick.

There is an easier way to convert your JSON element to a series of objects. Have you tried the Google GSon library? There is a sample:
import com.google.gson.Gson;
Gson gson = new Gson();
Handpoint testing = gson.fromJson(data, Handpoint.class);
System.out.println("Amount: " + testing.getAmount());
On the other hand, if you want to deserialize the dates, that contain arrays, you'd better take a look here:
Gson Array deserialization

Related

Convert JSON object to Java object with different format using GSON

I have a response that returns a json object in following format:
{
"playerId": "001",
"name": "michel",
"age": 21,
"nation": "USA",
"ratings": [
{
"type": "speed",
"score": "0121"
},
{
"type": "accuracy",
"score": "85"
}
],
"teaminfo": {
"teamName": "HON",
"isValid": "true"
}
}
and I have a Java Class as :
public class MyRider {
public String playerId;
public String name;
public int age;
public String speed;
public String accuracy;
public String teamName;
public String isValid;
//getter, setter...
}
I want to map the JSON object into Java object using GSON.
I tried using JsonDeserializationContext deserialize, and it returned null for the nested values in JSON.
Without custom deserializer
If you cannot change the JSON to return exactly what you want, I suggest you create classes to match it:
MyRider:
public class MyRider {
private String playerId;
private String name;
private int age;
private String nation;
private List<Rating> ratings;
private TeamInfo teaminfo;
// getters, setters, toString override
}
Rating:
public class Rating {
private String type;
private String score;
// getters, setters, toString override
}
TeamInfo:
private static class TeamInfo {
private String teamName;
private String isValid;
// getters, setters, toString override
}
Then simply deserialize as normal:
MyRider rider = gson.fromJson(json, MyRider.class);
If you need exactly the fields you've specified in MyRider in your question, consider a transformer class to map the full class above to your needs.
With custom deserializer
It's also possible to do this with a custom deserializer, but slightly pointless as GSON provides the normal mapping for you which you can then adapt.
Here is an example with a deserializer:
public class MyRiderDeserializer implements JsonDeserializer<MyRider> {
#Override
public MyRider deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
MyRider rider = new MyRider();
if(json.isJsonObject()) {
JsonObject riderObj = json.getAsJsonObject();
rider.setPlayerId(riderObj.get("playerId").getAsString());
rider.setName(riderObj.get("name").getAsString());
rider.setAge(riderObj.get("age").getAsInt());
JsonArray ratingsArray = riderObj.get("ratings").getAsJsonArray();
for(JsonElement ratingElem : ratingsArray) {
JsonObject ratingObj = ratingElem.getAsJsonObject();
String type = ratingObj.get("type").getAsString();
switch(type) {
case "speed":
rider.setSpeed(ratingObj.get("score").getAsString());
break;
case "accuracy":
rider.setAccuracy(ratingObj.get("score").getAsString());
break;
default:
break;
}
}
JsonObject teamInfo = riderObj.get("teaminfo").getAsJsonObject();
rider.setTeamName(teamInfo.get("teamName").getAsString());
rider.setIsValid(teamInfo.get("isValid").getAsString());
}
return rider;
}
}
Note this does not include any checks to validate whether the properties are actually there and is the simplest possible custom deserializer I could think of. To use it, you must register the type adapter at Gson creation time:
Gson gson = new GsonBuilder()
.registerTypeAdapter(MyRider.class, new MyRiderDeserializer())
.create();
MyRider myRider = gson.fromJson(reader, MyRider.class);

Creating a json for a post request

I am trying to send JSON string to post method in order to receive flights information, i have the following json example i must modify to make different requests
{
"request": {
"passengers": {
"adultCount": 1
},
"slice": [
{
"origin": "BOS",
"destination": "LAX",
"date": "YYYY-MM-DD"
},
{
"origin": "LAX",
"destination": "BOS",
"date": "YYYY-MM-DD"
}
]
}
}
And i have the following class
public class Request {
public class Passengers{
private int adultCount;
public int getAdultCount() {
return adultCount;
}
public void setAdultCount(int adultCount) {
this.adultCount = adultCount;
}
}
private List<Slice> slice;
public List<Slice> getSlice() {
return slice;
}
public void setSlice(List<Slice> slice) {
this.slice = slice;
}
public static class Slice{
private String origin;
private String destination;
private String date;
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
}
how do i map this class to set the values and create a different json with the same format? i am trying with Jackson but im getting no suitable output
Here's the JSON format. Learn it well.
A JSON string maps to a Java String. A JSON number maps to a Java Number type. A JSON array maps to a Collection type or an array type. A JSON object maps to a Java Map type or any other Java type (not mentioned previously).
So a Java class like
class Foo {
String name;
int[] array;
Map<String, List<Double>> ref;
}
maps to
{
"name" : "string",
"array" : [1, 2, 3],
"ref" : {
"val1" : [1.2, 3.4]
}
}
Your JSON is a JSON object at the root. It contains
"request": {
which is a key value pair where the key is a JSON string and the value is a JSON object.
This should tell you that you need a Java object with a field named request which should be referencing another object. Do this analysis recursively and you will get your Java Object formats.
It would greatly simplify your life using Gson, a json library from google wich makes all parsing/encoding based o your class diagram. It also supports recursivity wich let you define more complex structures such arraylist, list, hashtable. Everything works by its self.
private String field = "field_1";
priavate String field2 = "field_2";
ArraList<FIELDS> field_list = new ArrayList<FIELDS>();
Would traduce to :
{
field : "field_1",
field2 : "field_2"
field_list : [
etc...
]
}

How to split a String result from a JSON URL to individual java objects

I have wrote code that returns data of a JSON url.
The data is stored as a String and here is an example of the output;
{
"status": "success",
"records": [
{
"timestamp": 1381312251599,
"deviceId": "288",
"temperature": 17
},
{
"timestamp": 1381312281599,
"deviceId": "288",
"temperature": 17
},
{
"timestamp": 1381312311599,
"deviceId": "288",
"temperature": 17
}
]
}
here is a sample of the code used to get this information;
String jsonString = callURL("http://localhost:8000/eem/api/v1/metrics/temperature/288");
System.out.println(jsonString);
What I need help for is creating a Status field and then a records array which will hold the Timestamp, DeviceId, Temperature and there values.
I have tried looking at GSON, but I can't understand it
If anyone had any help, it would be great, thanks
Copy, paste and run this:
package stackoverflow.questions;
import java.util.List;
import com.google.gson.Gson;
public class Question {
class Record {
Long timestamp;
String deviceId;
Long temperature;
}
class Container {
List<Record> records;
}
public static void main(String[] args) {
String json = "{ \"status\": \"success\", \"records\": [{\"timestamp\": 1381222871868,\"deviceId\": \"288\",\"temperature\": 17 },{\"timestamp\": 1381222901868,\"deviceId\": \"288\",\"temperature\": 17 },{\"timestamp\": 1381222931868,\"deviceId\": \"288\",\"temperature\": 17 } ]} ";
Gson g = new Gson();
Container c = g.fromJson(json, Container.class);
for (Record r : c.records)
System.out.println(r);
}
}
It's pretty simple. you should create a java class matching your json structure.
e.g.
public class Response {
String status;
List<Record> records;
}
public class Record {
long timestamp;
int deviceId;
int temperature;
}
And than provide your json and Response.class to gson.
I use jackson and it's quite simple.
You need to create appropriate java classes which have same properties as your jsonString has.
You need to create something like this
class Record {
private Long timestamp;
private Integer deviceId;
private Integer temperature;
// getters and setters ...
}
class Response {
private String status;
private List<Record> records;
// getters and setters ...
}
and then
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(jsonString, Response.class);

Can I ignore just a setter w/ Jackson & Spring?

I have a situation where I am passing back n forth an object from Java to Javascript client and it's being serialized by the built in Jackson mapper in Spring 3 (using the #RequestBody / #ResponseBody and application/json content type)
The problem I have is some classes implement from an interface which has a getter but no setter.
I do want the getter value available from the client side so I cannot use #JsonIgnore annotation because then it ignores the property entirely, both serializing and deserializing. I need the property when serialized.
Any other way to do this?
There is probably an easier way, but I thought to mention the usage of JSON views as a possible solution. There's an example on this thread.
You might need a different view on deserialization and not just on serialization, and that would be a Jackson 2.0 feature - supported by Spring 3.2 and backported into Spring 3.1. Using a view on serialization only is a feature since Jackson 1.4.
Another option that comes to mind is using a custom deserializer.
I was looking for the same thing.
According to the Jackson docs, we can set a #JsonIgnore annotation on a property, getter or setter and that would hide the complete property, unless we give the setter a #JsonProperty annotation that would make sure this property is visible when setting it. Unfortunately, that doesn't work the other way around. I wanted to show the property in my response, but not include it in my request.
Use case is an auto generated id for example. I don't want the user to set, or even see that, but he may read it.
You can achieve this by hiding the attribute itself indeed and then add a #JsonAnyGetter method. In this method you can massage the data to any form you want. It's also useful to represent complex attribute classes of which you only want to show a single name or identifier or other formatting. There are better ways of doing the latter, but if the use case is not too complex, this suffices.
So as example (My apologies if this is too elaborate):
User:
public class User {
private String uid;
private String customerId;
private String taxSsnId;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getCustomerId() {
return extCustomerId;
}
public void setCustomerId(String extCustomerId) {
this.extCustomerId = extCustomerId;
}
public String getTaxSsnId() {
return taxSsnId;
}
public void setTaxSsnId(String taxSsnId) {
this.taxSsnId = taxSsnId;
}
#JsonIgnore
public void getId(){
if (getUid() != null){
return getUid();
}
if (getCustomerId() != null ){
return getCustomerId();
}
return null;
}
}
Setting:
public class Setting {
#JsonIgnore
private int id;
private String key;
private String value;
#JsonIgnore
private User lastUpdatedBy;
#JsonIgnore
private Date lastUpdatedAt;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public User getLastUpdatedBy() {
return lastUpdatedBy;
}
public void setLastUpdatedBy(User lastUpdatedBy) {
this.lastUpdatedBy = lastUpdatedBy;
}
public Date getLastUpdatedAt() {
return lastUpdatedAt;
}
public void setLastUpdatedAt(Date lastUpdatedAt) {
this.lastUpdatedAt = lastUpdatedAt;
}
#JsonAnyGetter
private Map<String, String> other() {
Map<String, String> map = new LinkedHashMap<String, String>();
map.put( "id", this.getId());
map.put( "lastUpdatedBy", this.getLastUpdatedBy().getId());
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
map.put( "lastUpdatedAt", format.format(this.getLastUpdatedAt()) );
return map;
}
}
Yields this Request schema (de-serialization view):
{
"key": "string",
"value": "string"
}
and this Response schema (serialized view):
{
"key": "string",
"value": "string",
"id": "12345",
"lastUpdatedBy": "String",
"lastUpdatedAt": "String"
}

Using gson to parse json to java object

I want to parse a json data into a java object, then post it to a .jsp to then turn it back into a json object so I can put it into a javascript array.
Here is an example of the data I want to parse:
"LoanList" = [ {"Loan" : "One","Lat" : "35.65365", "Lng" : "123.1331" , "Bal" : "4565" , "Address" : "Address 1" , "Delinquency" : "True')]
After reading about it, I decided to use gson to parse the data: After reading other questions about this topic, I wrote two classes.
class1.java
import java.util.ArrayList;
public class JSON1 {
ArrayList<innerData> Loanlst;
public ArrayList<innerData> getLoanlst() {
return Loanlst;
}
public void setLoanlst(ArrayList<innerData> Loanlst) {
this.Loanlst = Loanlst;
}
}
class2.java
import java.math.*;
public class innerData {
public String loan;
public BigDecimal lat;
public BigDecimal lng;
public BigDecimal bal;
public String addrs;
public String delinq;
public String getLoan() {
return loan;
}
public void setLoan(String loan){
this.loan = loan;
}
public BigDecimal getLat() {
return lat;
}
public void setLat(BigDecimal lat){
this.lat = lat;
}
public BigDecimal getLng() {
return lng;
}
public void setLng(BigDecimal lng){
this.lng = lng;
}
public BigDecimal getBal() {
return bal;
}
public void setBal(BigDecimal bal){
this.bal = bal;
}
public String getAddrs() {
return addrs;
}
public void setAddrs(String addrs){
this.addrs = addrs;
}
public String getDelinq() {
return delinq;
}
public void setDelinq(String delinq){
this.delinq = delinq;
}
}
Here is where I am stuck, Where do I create my new Gson() to parse the data before I send it?
Using your class you should be able to do something like:
InnerData obj = new InnerData(...);//fill in with data
Gson gson = new Gson();
// convert java object to JSON format,
// and returned as JSON formatted string
String json = gson.toJson(obj);
check an example here and here
Tip: try creating a main class to run it separately.
try this:
JsonParser parser = new JsonParser();
JsonObject jsonObject=parser.parse("your string").getAsJsonObject();
also if it is a jsonArray ,change getAsJsonObject to getAsJsonArray
also jackson offers an easy to to parser string to json
you can search jackson apache

Categories