Carlos Jaime C. De Leon
Carlos Jaime C. De Leon

Reputation: 2896

Generate Java Code that populates all fields from a JSON file

The reason for this is in my Selenium tests, I am mocking the REST services to return POJOs with hardcoded values, which represents my dummy data. One of the pages requires a list of objects who has heaps of fields and children Java objects (think Person has List, List, etc.).

A quick way I did was generate a JSON string from one of the REST services that pulls from the database. So now, I have a JSON string which I saved as a file and can load into my Selenium test as my hardcoded data. However, I want to maintain this in the Java code rather than a separate file, the data.json file.

Is there a way to generate Java code, which is basically lines and lines of setters where the values come from the JSON? I am trying to avoid having to hand-code each setter for each fields....

Example json file (in reality it has more fields and more children...):

{
   "personEntity":{
      "name":"xxx",
      "dob":"2000-01-01",
      "address":[
         {
            "id":"1",
            "line1":"123"
         },
         {
            "id":"2",
            "line1":"zzz"
         }
      ],
      "phones":[
         {
            "id":"1",
            "number":"999-999-999"
         }
      ]
   }
}

Desired Java code that is auto-generated:

Person p = new Person();
p.setName("xxx");
p.setDob("2000-01-01");
Address a1 = new Address();
a1.setId(1);
a1.setLine1("123")
p.addAddress(a1);
// and so on for the other fields

NOTE:

The POJOs are already existing and are NOT needed to be auto-generated. The only auto-generated code I am looking for is the sample above such as p.setName("xxx") and so on for the other fields.

Upvotes: 4

Views: 1796

Answers (2)

GrabNewTech
GrabNewTech

Reputation: 641

You need to de-serialize the returned JSON to java object using any of the parsers like GSON, Jackson, JSON simple etc.

Some online tools available to do your job very simple. You can use jsonschema2pojo

-----------------------------------com.example.Address.java-----------------------

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"line1"
})
public class Address {

@JsonProperty("id")
private String id;
@JsonProperty("line1")
private String line1;

@JsonProperty("id")
public String getId() {
return id;
}

@JsonProperty("id")
public void setId(String id) {
this.id = id;
}

@JsonProperty("line1")
public String getLine1() {
return line1;
}

@JsonProperty("line1")
public void setLine1(String line1) {
this.line1 = line1;
}

}
and so on....

Upvotes: -1

Jason Christ
Jason Christ

Reputation: 125

Do your mean JSON -> JAVA Bean? You can use this website json2javapojo

then you can use JSON utils to parse.

package ;
public class Address {
private String id;

private String line1;

public void setId(String id){
this.id = id;
}
public String getId(){
return this.id;
}
public void setLine1(String line1){
this.line1 = line1;
}
public String getLine1(){
return this.line1;
}

}

package ;
public class Phones {
private String id;

private String number;

public void setId(String id){
this.id = id;
}
public String getId(){
return this.id;
}
public void setNumber(String number){
this.number = number;
}
public String getNumber(){
return this.number;
}

}


package ;
import java.util.List;
public class PersonEntity {
private String name;

private String dob;

private List<Address> address ;

private List<Phones> phones ;

public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setDob(String dob){
this.dob = dob;
}
public String getDob(){
return this.dob;
}
public void setAddress(List<Address> address){
this.address = address;
}
public List<Address> getAddress(){
return this.address;
}
public void setPhones(List<Phones> phones){
this.phones = phones;
}
public List<Phones> getPhones(){
return this.phones;
}

}

package ;
public class Root {
private PersonEntity personEntity;

public void setPersonEntity(PersonEntity personEntity){
this.personEntity = personEntity;
}
public PersonEntity getPersonEntity(){
return this.personEntity;
}

}

Upvotes: -1

Related Questions