Skip to content

Commit 94e1d00

Browse files
authored
Create 2024-06-25 - import a list of json files.pq
1 parent e7370dc commit 94e1d00

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
#"Expanded Json" = Table.ExpandRecordColumn(Column_ConvertJson,
38+
"Json",
39+
{"Table", "HasError" },
40+
{"Table", "HasError" } )
41+
42+
// now you have an array of json tables
43+
in
44+
#"Expanded Json"

0 commit comments

Comments
 (0)