Simple Steps To Read JSON file and save it to database in spring boot
Posted By : S. Ajit | 09-Apr-2017
This blog assumes that you are using spring-boot maven project and you are familier with spring data JPA.
In This blog we are going to use following packages for converting JSON to POJO and save this POJO into the database.
1. jackson-databind : A package that convert JSON to and from POJOs. To get jackson-databind add following dependency into your pom.xml file
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
2. jackson-core : A package that contains core low-level incremental streaming parser and generator abstractions used by Jackson Data Processor. To get jackson-core add following dependency into your pom.xml file.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
To map JSON to POJO we use ObjectMapper class which is available in com.fasterxml.jackson.databind package to save POJO to database and we will use spring-data JPA .
Lets assume that you have json file which has list of states, pincode regex and its respective cities. The JSON will look like this:
[{
"name": "Andaman and Nicobar Islands",
"pincodeRegex":"^744[0-9]{3}$",
"city": [{
"name": "Nicobar"
}, {
"name": "North And Middle Andaman"
}, {
"name": "South Andaman"
}]
}, {
"name": "Andhra Pradesh",
"pincodeRegex":"^5[1-3][0-9]{4}$",
"city": [{
"name": "Adoni"
}, {
"name": "Amadalavalasa"
},
.
.
.
.
},
{...},
{...}
]
Based on this JSON Lets create State.java entity and City.java entity
/************* State.java ***************/
@Entity
public class State {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
private String pincodeRegex;
@OneToMany(cascade = {CascadeType.ALL})
private List<City> city;
// getter and setter methods
}
/************** City.java *************/
@Entity
public class City {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
// getter and setter methods
}
Lets create a repository for State entity:
public interface StateRepository extends JpaRepository<State, Serializable>{
}
Now lets autowire StateRepository in your service:
@Autowired
StateRepository stateRepository;
Assuming that you have JSON file say state-city.json in json folder which is present inside resources folder of your spring boot application. we can use TypeReference class of com.fasterxml.jackson.core.type package to stream JSON file which is of type List<State> and we will use this stream to map it into object using ObjectMapper class. After mapping the JSON to Object we will use stateRepository to save it to database. so our final code will be like this:
ObjectMapper mapper = new ObjectMapper();
TypeReference<List<State>> mapType = new TypeReference<List<State>>() {};
InputStream is = TypeReference.class.getResourceAsStream("/json/state-city.json");
try {
List<State> stateList = mapper.readValue(is, mapType);
stateRepository.save(stateList);
System.out.println("States list saved successfully");
} catch (IOException e) {
System.out.println(e.getMessage());
}
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
S. Ajit
Ajit is a software developer who loves solving problems and programming in C, C++, Java. He also has a passion to learn new technologies.