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?

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
    – Akina
    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.
    – Akina
    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. Mar 23, 2021 at 9:51
  • See here or here
    – user1822
    Mar 23, 2021 at 12:08

0

Your Answer

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

Browse other questions tagged or ask your own question.