2

I have a JSONObject as shown below, in which the fields may vary at run time. I need to map this to a jsonb datatype field in postgres using jpa entity and to retrieve the same to JSONOject using JPARepository.

 JSONObject jsonObj = new JSONObject();
     jsonObj.put("name", "Tom");
     jsonObj.put("birthday", "1940-02-10");
     jsonObj.put("age", 76);
     jsonObj.put("married", false);
     jsonObj.put("favorite_foods", new String[] { "cookie", "fish", 
     "chips" });
4
  • First, I'm pretty sure you can't do that if you want to have String[] array in there - use JSONArray. Commented Feb 5, 2019 at 10:44
  • Possible duplicate of How to use Postgres JSONB datatype with JPA? Commented Feb 5, 2019 at 10:44
  • The above solution didn't work for me as it ends up in the same error mentioned by Seweryn Niemiec. But find a solution using JsonNode as a custom usertype (SqlTypeDescriptor). Commented Feb 11, 2019 at 9:58
  • If your solution works, do post it on quesion I linked (assuming it's not posted already there, otherwise - upvote the answer you've used). Commented Feb 11, 2019 at 12:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.