ToJson

Description

Returns the input as a JSON string.

Syntax

ToJson(input[, try_parse])

Arguments

   input - input field can be of the following data types: string, int, long, float, double, datetime, boolean, map or bag.

   try_parse - true by default. Determines whether to try and parse the input string as JSON. If the string input can be parsed as JSON, use the parsed JSON in the output string. Otherwise, encode the string as a JSON string. If set to false, any string input would be returned as a JSON encoded string (see examples below).

Examples

With try_parse set to true (default value):

    • ToJson(null) returns 'null'
    • ToJson(1) returns '1'
    • ToJson(CurrentTime()) returns '"2018-02-21T08:59:00.000Z"'
    • ToJson(true) returns 'true'
    • ToJson('true') returns 'true'
    • ToJson('abc') returns '"abc"'
    • ToJson('"a"') returns '"a"'
    • ToJson('{"a":1}') returns '{"a":1}'
    • ToJson('[1,2,3]') returns '[1,2,3]'
    • ToJson('a"b"c') returns '"a\"b\"c"'
    • ToJson(TOMAP('a',1,'b','c')) -> '{"a":1,"b":"c"}'

  • ToJson(TOMAP('a','1','b','c')) -> '{"a":1,"b":"c"}'

  • ToJson(TOMAP('a','{"c":1}','b','c')) -> '{"a":{"c":1},"b":"c"}'

  • ToJson(TOMAP('a','{"c":1}','b',TOMAP('d',1))) -> '{"a":{"c":1},"b":{"d":1}}'

  • ToJson(TOBAG(1,2,3)) returns '[1,2,3]'
  • ToJson(TOBAG(TOTUPLE(1),TOTUPLE(2),TOTUPLE(3))) returns '[1,2,3]'
  • ToJson(TOBAG('{"a":1}','{"a":2}')) returns '[{"a":1},{"a":2}]'
  • ToJson(TOBAG(TOMAP('a',1),TOMAP('a',2))) returns '[{"a":1},{"a":2}]'
  • ToJson(TOBAG(TOTUPLE(1,2,3),TOTUPLE(4,5,6))) will throw an exception as tuples are not supported.

With try_parse set to false:

    • ToJson(null,false) returns 'null'
    • ToJson(1,false) returns '1'
    • ToJson('1',false) returns '"1"'
    • ToJson('true',false) returns '"true"'
    • ToJson('abc',false) returns '"abc"'
    • ToJson('"a"',false) returns '"\\"a\\""'
    • ToJson('{"a":1}',false) returns '"{\\"a\\":1}"'
    • ToJson('[1,2,3]',false) returns '"[1,2,3]"'
    • ToJson('a"b"c', false) returns '"a\"b\"c"'
    • ToJson(CurrentTime(),false) returns '"2018-02-21T08:59:00.000Z"'
    • ToJson(true, false) returns 'true'
    • ToJson(TOMAP('a',1,'b','c'),false) -> '{"a":1,"b":"c"}'

  • ToJson(TOMAP('a','1','b','c'),false) -> '{"a":"1","b":"c"}'

  • ToJson(TOMAP('a','{"c":1}','b','c'),false) -> '{"a":"{\\"c\\":1}","b":"c"}'

  • ToJson(TOMAP('a','{"c":1}','b',TOMAP('d',1)),false) -> '{"a":"{\\"c\\":1}","b":{"d":1}}'

  • ToJson(TOBAG(1,2,3),false) returns '[1,2,3]'
  • ToJson(TOBAG(TOTUPLE(1),TOTUPLE(2),TOTUPLE(3)),false) returns '[1,2,3]'
  • ToJson(TOBAG('{"a":1}','{"a":2}'),false) returns '["{\\"a\\":1}","{\\"a\\":2}"]'
  • ToJson(TOBAG(TOMAP('a',1),TOMAP('a',2)),false) returns '[{"a":1},{"a":2}]'
  • ToJson(TOBAG(TOTUPLE(1,2,3),TOTUPLE(4,5,6)),false) will throw an exception as tuples are not supported

Notes

Tuples with more than one field are not supported.

Return value datatype

String

Impact of null value

If input is null, returns 'null' as string.