Postgres function to merge two json objects with overlapping keys into one object
If you're using PostgreSQL 9.5 (at least), you can use the concatenation operator (||) on jsonb types (and you can convert json to jsonb if necessary first).

For example:

WITH test(data) AS (
    VALUES ('{
        "a" : {
            "0" : 2,
            "1" : 4,
            "3" : 6
        },
        "b" : {
            "2" : 8,
            "1" : 10,
            "4" : 12
        }
    }'::jsonb)
)
SELECT (data-&gt;'a') || (data-&gt;'b') FROM test


will produce:

{"0": 2, "1": 10, "2": 8, "3": 6, "4": 12}


(Please note that, in this particular example, the parentheses around (data-&gt;'a') matter.)

You can modify the example above to get a specific value, as you requested, for example:

SELECT (((data-&gt;'a') || (data-&gt;'b'))-&gt;'1')::text::integer FROM test