Skip to content

Commit

Permalink
Create 2024-06-25 - import a list of json files.pq
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey authored Jun 25, 2024
1 parent e7370dc commit 94e1d00
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions forumQuestions/pq/2024-06-25 - import a list of json files.pq
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"

0 comments on commit 94e1d00

Please sign in to comment.