Skip to content

Commit 36aa9dd

Browse files
committed
2 parents 80939a7 + e96ef2b commit 36aa9dd

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let
2+
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtE1MNY1NFGK1YlWArN0QYJKsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DateString = _t]),
3+
#"Changed Type" = Table.TransformColumnTypes(Source,{{"DateString", type text}}),
4+
format1 = "yyyy-MM-dd",
5+
format2 = "MM-dd-yyyy",
6+
7+
#"Added Custom" = Table.AddColumn(#"Changed Type", "Date",
8+
(row) =>
9+
try
10+
Date.FromText( row[DateString], [ Format = format1, Culture = "en-us" ] )
11+
catch (e1) =>
12+
try
13+
Date.FromText( row[DateString], [ Format = format2, Culture = "en-us" ] )
14+
catch (e2) => error "Failed both dates",
15+
type date
16+
)
17+
in
18+
#"Added Custom"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
let
2+
/*
3+
about: this creates example json files inline, without requiring files to exist
4+
source: <https://github.com/ninmonkey/ninMonkQuery-examples/new/main/forumQuestions/pq/2024-06-25 - import a list of json files.pq>
5+
*/
6+
Source =
7+
let
8+
FakeJson1 = {
9+
[ name = "Jen", id = 2222 ],
10+
[ name = "Stan", id = 1111 ]
11+
},
12+
FakeJson2 = {
13+
[ region = "West", color = "blue"],
14+
[ region = "South", color = "salmon" ]
15+
},
16+
FakeFiles = {
17+
[ filename = "employees.json", Content = Json.FromValue( FakeJson1 ) ],
18+
[ filename = "region.json", Content = Json.FromValue( FakeJson2 ) ],
19+
[ filename = "shouldFail.json", Content = null ]
20+
}
21+
in
22+
Table.FromRecords( FakeFiles, type table [ filename = text, Content = binary ] ) ,
23+
24+
// real code starts here.
25+
Column_ConvertJson = Table.AddColumn(
26+
Source,
27+
"Json",
28+
(row) => [
29+
// I threw in extra record fields to make errors easier to read
30+
// you only need the field "Table"
31+
JsonList = Json.Document( row[Content] ),
32+
Table = Table.FromRecords( JsonList ),
33+
HasError = (try Table)[HasError]
34+
],
35+
type [ JsonList = list, Table = Table.Type, HasError = logical ]
36+
),
37+
// if you want json to be a table, it expects it to be an array (aka list) at
38+
// the top level. if yours is not, you'll need to either shape it, or drill down into an array.
39+
40+
#"Expanded Json" = Table.ExpandRecordColumn(Column_ConvertJson,
41+
"Json",
42+
{"Table", "HasError" },
43+
{"Table", "HasError" } )
44+
45+
// now you have an array of json tables
46+
in
47+
#"Expanded Json"

0 commit comments

Comments
 (0)