0

Here I get JSON Data from my Local Directory Using JsonObject, What can I do To Store this JsonObject To my Postgresql Database?

Example data:

{
  "emp_id":1017,
  "emp_name":"Nagesh",
  "emp_designation":"Manager",
  "department":"Java2Novice",
  "salary":30000,
  "direct_reports":["Nataraj G","Kalyan","Mahitha"],
  "address": {"street":"MG Road","city":"Bangalore"}
}

I have proper Application.property for Postgresql Connection

spring.datasource.url=jdbc:postgresql://localhost:5432/springjson
spring.datasource.username=postgres
spring.datasource.password=secret
spring.jpa.show-sql=true

how can I store in db

@SpringBootApplication
public class SpringNestedJsonToDbApplication {

public static void main(String[] args) {

        SpringApplication.run(SpringNestedJsonToDbApplication.class,args);

  File jsonInputFile = new File("D:/sample.json");

            InputStream is;

            try {

                is = new FileInputStream(jsonInputFile);
                

                JsonReader reader = Json.createReader(is);
                

                JsonObject empObj = reader.readObject();
                System.out.println(empObj);
                reader.close();
             
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
1

1 Answer 1

0

Use the .toString() method of JSONObject to get the text and save.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.