MAPTOSTRING
Recursively builds a string representation VMap data, including nested JSON maps. Use this transform function to display the VMap contents in a readable LONG VARCHAR format. Use maptostring to see how map data is nested before querying virtual columns with mapvalues().
Syntax
MAPTOSTRING ( VMap-data [ USING PARAMETERS canonical_json={true | false} ] )
Arguments
VMap-data
|
Any VMap data. The VMap can exist as:
|
Parameters
canonical_json
|
=bool Produces canonical JSON output by default, using the first instance of any duplicate keys in the map data. Use this parameter as other UDF parameters, preceded by Default value: |
Examples
The following example shows how to create a sample flex table, darkdataand load JSON data from STDIN. By calling maptostring() twice with both values for the canonical_json parameter, you can see the different results on the flex table __raw__ column data.
- Create sample table:
- Load sample JSON data from STDIN:
- Call
maptostring()with its default behavior using canonical JSON output, and then review the flex table contents. The function returns the first duplicate key and its value ("aaa": "1") but omits remaining duplicate keys ("aaa": "2"): - Next, call
maptostring()withusing parameters canonical_json=false). This time, the function returns the first duplicate keys and their values:
=> CREATE FLEX TABLE darkdata(); CREATE TABLE
=> COPY darkdata FROM stdin parser fjsonparser();
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> {"aaa": 1, "aaa": 2, "AAA": 3, "bbb": "aaa\"bbb"}
>> \.
=> SELECT MAPTOSTRING (__raw__) FROM darkdata;
maptostring
------------------------------------------------------------
{
"AAA" : "3",
"aaa" : "1",
"bbb" : "aaa\"bbb"
}
(1 row)
=> SELECT MAPTOSTRING(__raw__ using parameters canonical_json=false) FROM darkdata;
maptostring
---------------------------------------------------------------
{
"aaa": "1",
"aaa": "2",
"AAA": "3",
"bbb": "aaa"bbb"
}
(1 row)