-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 2024-06-25 - import a list of json files.pq
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
forumQuestions/pq/2024-06-25 - import a list of json files.pq
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
let | ||
/* | ||
about: this creates example json files inline, without requiring files to exist | ||
source: <https://github.com/ninmonkey/ninMonkQuery-examples/new/main/forumQuestions/pq/2024-06-25 - import a list of json files.pq> | ||
*/ | ||
Source = | ||
let | ||
FakeJson1 = { | ||
[ name = "Jen", id = 2222 ], | ||
[ name = "Stan", id = 1111 ] | ||
}, | ||
FakeJson2 = { | ||
[ region = "West", color = "blue"], | ||
[ region = "South", color = "salmon" ] | ||
}, | ||
FakeFiles = { | ||
[ filename = "employees.json", Content = Json.FromValue( FakeJson1 ) ], | ||
[ filename = "region.json", Content = Json.FromValue( FakeJson2 ) ], | ||
[ filename = "shouldFail.json", Content = null ] | ||
} | ||
in | ||
Table.FromRecords( FakeFiles, type table [ filename = text, Content = binary ] ) , | ||
|
||
// real code starts here. | ||
Column_ConvertJson = Table.AddColumn( | ||
Source, | ||
"Json", | ||
(row) => [ | ||
// I threw in extra record fields to make errors easier to read | ||
// you only need the field "Table" | ||
JsonList = Json.Document( row[Content] ), | ||
Table = Table.FromRecords( JsonList ), | ||
HasError = (try Table)[HasError] | ||
], | ||
type [ JsonList = list, Table = Table.Type, HasError = logical ] | ||
), | ||
#"Expanded Json" = Table.ExpandRecordColumn(Column_ConvertJson, | ||
"Json", | ||
{"Table", "HasError" }, | ||
{"Table", "HasError" } ) | ||
|
||
// now you have an array of json tables | ||
in | ||
#"Expanded Json" |