Skip to main content
Question

Is the vertica has the function that converts row to JSON format.?

  • March 22, 2021
  • 3 replies
  • 9 views

HyeontaeJu
Forum|alt.badge.img+2

Is the vertica has the function that converts row to JSON format.?

  • The table is not a flex table*
    example)
    select * from table;
    ** general result**
    id text
    1 amy
    2 john

if i use the json function
select json(*) from table;
** json result **

result
{'id':1, 'text':'amy'}
{'id':2, 'text':'john'}

3 replies

SergeB
Forum|alt.badge.img
  • Participating Frequently
  • March 22, 2021

You can use the vmap functions to build a vmap for each row and then get its JSON representation.

 select maptoString(mapput(emptymap(),id,"text" using parameters keys=SetMapKeys('id','text'))) as json_string from foo;
              json_string
---------------------------------------
 {
    "id": "1",
      "text": "amy"
}
 {
    "id": "2",
    "text": "john"
}

HyeontaeJu
Forum|alt.badge.img+2
  • Author
  • Participating Frequently
  • March 23, 2021

Oh.. Thank you so much.. @SergeB
I have one more question..
If i need all column,, then, i have to write all column in the function??


SergeB
Forum|alt.badge.img
  • Participating Frequently
  • March 23, 2021

@HyeontaeJu Yes, you will need to list all the columns you wish to include in your JSON result.