Created
July 8, 2015 22:52
-
-
Save CurtHagenlocher/353a8384f1c5adca7cb9 to your computer and use it in GitHub Desktop.
Format M data as JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
ByteToHex = (i as number) as text => | |
let | |
chars = "0123456789abcdef", | |
low = Text.Range(chars, Number.Mod(i, 16), 1), | |
high = Text.Range(chars, Number.RoundDown(i / 16), 1) | |
in high & low, | |
Json.EscapeChar = (text as text) as text => | |
if text = """" or text = "\" or text = "/" then "\" & text | |
else if Character.ToNumber(text) < 32 then "\u00" & ByteToHex(Character.ToNumber(text)) | |
else text, | |
Json.FormatText = (text as text) as text => """" & Text.Combine(List.Transform(Text.ToList(text), Json.EscapeChar)) & """", | |
Json.FormatLogical = (value as logical) as text => if value then "true" else "false", | |
Number.IsNumber = (value as number) as logical => not Number.IsNaN(value) and value <> Number.PositiveInfinity and value <> Number.NegativeInfinity, | |
Json.FormatNumber = (value as number) as text => if Number.IsNumber(value) then Text.From(value, "") else error Error.Record("Expression.Error", "Not a number", value), | |
Json.FormatList = (value as list) as text => "[" & Text.Combine(List.Transform(value, Json.Format), ",") & "]", | |
Json.FormatPair = (key as text, record as record) as text => Json.FormatText(key) & ":" & Json.Format(Record.Field(record, key)), | |
Json.FormatRecord = (value as record) as text => "{" & Text.Combine(List.Transform(Record.FieldNames(value), (key) => Json.FormatPair(key, value)), ",") & "}", | |
Json.Format = (value) as text => | |
if value = null then "null" | |
else if value is text then Json.FormatText(value) | |
else if value is logical then Json.FormatLogical(value) | |
else if value is number then Json.FormatNumber(value) | |
else if value is list then Json.FormatList(value) | |
else if value is record then Json.FormatRecord(value) | |
else if value is table then Json.FormatList(Table.ToRecords(value)) | |
else error Error.Record("Expression.Error", "Unsupported value", value) | |
in | |
Json.Format(Table.FromRows({{"A", "B", "C"}, {1, 2, 3}})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now that Json.FromValue is in the standard library, this is probably less interesting.