1

I have two jsons as follows

JSON 1 -

{
"2021年03月11日": [
 {
 "14:30:00": 3000
 }
],
"2021年03月14日": [
 {
 "14:30:00": 130
 }
]

}

JSON 2 -

{
"2021年03月02日": [
 {
 "10:30:00": null
 }
],
"2021年03月11日": [
 {
 "10:30:00": null
 },
 {
 "11:30:00": null
 },
 {
 "14:30:00": null
 }
],
"2021年03月12日": [
 {
 "11:30:00": null
 }
],
"2021年03月14日": [
 {
 "14:30:00": null
 }
],
"2021年03月15日": [
 {
 "10:30:00": null
 }
]

}

I want to combine these two JSON like below

{
"2021年03月02日": [
 {
 "10:30:00": null
 }
],
"2021年03月11日": [
 {
 "10:30:00": null
 },
 {
 "11:30:00": null
 },
 {
 "14:30:00": 3000
 }
],
"2021年03月12日": [
 {
 "11:30:00": null
 }
],
"2021年03月14日": [
 {
 "14:30:00": 130
 }
],
"2021年03月15日": [
 {
 "10:30:00": null
 }
]

}

But when I use the || operator, this is the result I get

{
{
"2021年03月02日": [
 {
 "10:30:00": null
 }
],
"2021年03月11日": [
 {
 "14:30:00": 3000
 }
],
"2021年03月12日": [
 {
 "11:30:00": null
 }
],
"2021年03月14日": [
 {
 "14:30:00": 130
 }
],
"2021年03月15日": [
 {
 "10:30:00": null
 }
]

}

for some reason it only retrieves only one second level key value pair, is there anyway to fix this?

asked Mar 23, 2021 at 9:26
4
  • for some reason it only retrieves only one second level key value pair This is a norma: Concatenating two objects generates an object containing the union of their keys, taking the second object's value when there are duplicate keys. postgresql.org/docs/current/functions-json.html Commented Mar 23, 2021 at 9:46
  • Concatenation does not iterate over all document strusture, it operates only on the upper level. So you must unnest your JSONs then nest them back. Commented Mar 23, 2021 at 9:48
  • Have a look here for a starter: dba.stackexchange.com/questions/287155/… You'll have to add a level to deal with your nesting. Commented Mar 23, 2021 at 9:51
  • See here or here Commented Mar 23, 2021 at 12:08

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.