-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTable.ToJson.pq
30 lines (29 loc) · 1.44 KB
/
Table.ToJson.pq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
let
Documentation = type function (
source as (type table meta [
Documentation.FieldCaption = "Input Table",
Documentation.FieldDescription = "Input Table"
]),
optional encoding as (type nullable number meta [
Documentation.FieldCaption = "Text Encoding",
Documentation.FieldDescription = "Text Encoding",
Documentation.AllowedValues = { TextEncoding.Ascii, TextEncoding.BigEndianUnicode, TextEncoding.Unicode, TextEncoding.Utf16, TextEncoding.Utf8, TextEncoding.Windows }
])
) as table meta [
Documentation.Name = "TableToJson",
Documentation.LongDescription = "Converts a table to JSON. (The reverse of using 'Enter Data'). Currently this does not save schema other than JSON types",
Documentation.Examples = {[
Description = "Generate one value",
Code = "TableToJson( #table({""Animal"", ""Id""}, {{""Cat"", 1}, {""Turtle"", 2}}) )",
Result = "[{""Animal"":""Cat"",""Id"":1},{""Animal"":""Turtle"",""Id"":2}]"
]}
],
TableToJson = (source as table, optional encoding as nullable number) as text =>
let
encoding = encoding ?? TextEncoding.Utf8,
bin = Json.FromValue(source, encoding),
jsonAsText = Text.FromBinary(bin, encoding)
in
jsonAsText
in
Value.ReplaceType( TableToJson, Documentation)