toStdJson
Syntax
toStdJson(obj, [longAsString=false])
Details
Converts a DolphinDB object to JSON format.
Parameters
-
obj cannot be of data form matrix or pair, nor can it be of data type UUID, IPADDR, INT128, COMPRESSED, or system types.
-
longAsString is a Boolean scalar that specifies whether non-NULL LONG values are converted to JSON strings. The default is false, in which case non-NULL LONG values are output as JSON numbers. NULL values of type LONG are always output as JSON null.
Note:If you need to transmit very large integers in JavaScript, web front-end, REST API, or similar scenarios, you can setlongAsString=trueto prevent precision loss when values of type LONG are deserialized from JSON. After deserialization, you can use the long function to convert the STRING value back to a LONG.
Returns
A scalar of type STRING.
Examples
x=1 2 3
toStdJson(x);
// output: [1, 2, 3]
t=table(1 2 3 as id, 10 20 30 as val)
toStdJson(t);
// output: [{"id": 1,"val": 10},{"id": 2,"val": 20},{"id": 3,"val": 30}]
b = set(2012.06.13T13:30:10 2017.07.10T14:10:12)
toStdJson(b);
// output: ["2017.07.10 14:10:12","2012.06.13 13:30:10"]
b = dict(int,datetime)
b[0] = 2012.06.13 13:30:10
b[1] = 2017.07.10 14:10:12
toStdJson(b);
// output: {"1": "2017.07.10 14:10:12","0": "2012.06.13 13:30:10"}
t1=table(`x`y`z as b, 2012.06.13 13:30:10 2012.06.13 13:30:10 2012.06.13 13:30:10 as c,10.8 7.6 3.5 as F)
toStdJson(t1);
// output: [{"b": "x","c": "2012.06.13 13:30:10","F": 10.8},{"b": "y","c": "2012.06.13 13:30:10","F": 7.6},{"b": "z","c": "2012.06.13 13:30:10","F": 3.5}]
Set longAsString=true to prevent precision loss.
x = 3380900824313224451l
y1 = toStdJson(x)
// Output:3380900824313224451
y2 = toStdJson(x, true)
// Output:"3380900824313224451"
// Deserialize y1 and y2
res1 = long(fromStdJson(y1))
res2 = long(fromStdJson(y2))
x == res1 // Returns false due to precision loss
x == res2 // Returns true because no precision is lost
