-2

How to parse JSON

{
"35673": [
        "234",
        "357",
        "123"
    ],
    "34566": [
        "333",
        "456",
        "789"
    ]
}

to Greenplum table, format 1 key 1 value?

enter image description here

  • Welcome to Stack Overflow. Please take the [tour](https://stackoverflow.com/tour) to learn how Stack Overflow works and read [How to Ask](https://stackoverflow.com/help/how-to-ask) or how to improve the quality of your question. Then edit your question to include your full source code you have as a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), which can be compiled and tested by others. – zforgo Dec 27 '20 at 10:46

1 Answers1

1

In your basic example, there is no json nested object, no array of json object, and all the object values are arrays of string. So within these assumptions, you can use the json_path_query function with the jsonpath operator keyvalue() so that to split your json object into a set of key/value and then use the json_array_elements function so that to split the resulting values which are systematically 1-Dim arrays :

SELECT j->>'key' AS Key, json_array_elements(j->'value')->>0 AS Value
FROM json_path_query (your_json_object_here, '$.keyvalue()') AS j

The output for your example is :

Key     Value     
34566   333
34566   456
34566   789
35673   234
35673   357
35673   123

All these functions can apply to the jsonb type which is preferred to the json type, see the manual :

"The json and jsonb data types accept almost identical sets of values as input. The major practical difference is one of efficiency. The json data type stores an exact copy of the input text, which processing functions must reparse on each execution; while jsonb data is stored in a decomposed binary format that makes it slightly slower to input due to added conversion overhead, but significantly faster to process, since no reparsing is needed. jsonb also supports indexing, which can be a significant advantage."

If your json structure is more complex with nested objects or arrays of objects, you will have to adapt the function in the FROM clause. If your object values are not systematically arrays of string, you will have to adapt the function applied to the resulting json value field.

Edouard
  • 6,577
  • 1
  • 9
  • 20
  • SELECT j->>'key' AS Key, json_array_elements(j->'value')->>0 AS Value FROM json_path_query ('/Users/ternik/Downloads/data.json', '$.keyvalue()') AS j; SQL Error [42883]: ERROR: function json_path_query(unknown, unknown) does not exist No function matches the given name and argument types. You might need to add explicit type casts. – Chainik_1 Dec 27 '20 at 14:35
  • '/Users/ternik/Downloads/data.json' is not recognized as a json object by PostgreSQL. You should either manually copy & paste the content of the data.json file in this place, put it between simple quotes, and then add right after `:: json`, or you should import the content of the data.json file in a postgres table with the command [COPY FROM](https://www.postgresql.org/docs/13/sql-copy.html) and then refer to the json column of that table inside the json_path_query function – Edouard Dec 27 '20 at 20:01