Forum Discussion
Expanding Json list
- 5 years ago
Hi, John343467 , the solution is really verbose as requested fields lie in different layers of hierarchy. you might want to try this solution,
let Source = Json.Document(Web.Contents("https://www.tomtom.com/en_gb/traffic-index/page-data/stuttgart-traffic/page-data.json")), tbl = Table.FromRecords({Source[result][data][citiesJson]}), #"Removed Columns" = Table.SelectColumns(tbl, {"name"} & List.Select(Table.ColumnNames(tbl), each Text.Contains(_,"stats"))), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"name"}, "Year", "Record"), #"Extracted Yr" = Table.TransformColumns(#"Unpivoted Other Columns", {{"Year", each Number.From(Text.Select(_,{"0".."9"})), type number}}), #"Expanded Record" = Table.ExpandRecordColumn(#"Extracted Yr", "Record", {"rank", "congestion", "results"}, {"rank", "congestion", "results"}), results = Table.TransformColumns(#"Expanded Record", { {"results", each [ worst = [worstDay], best = [bestDay], res = [ #"worst Day/Month" = Text.From(worst[day])&"/"&Text.From(worst[month]), #"worst Congestion" = Number.Round(worst[congestion]), #"best Day/Month" = Text.From(best[day])&"/"&Text.From(best[month]), #"best Congestion" = Number.Round(best[congestion]) ] ][res] } } ), #"Expanded results" = Table.ExpandRecordColumn(results, "results", {"worst Day/Month", "worst Congestion", "best Day/Month", "best Congestion"}, {"worst Day/Month", "worst Congestion", "best Day/Month", "best Congestion"}) in #"Expanded results"
parry2k Thanks. I'm looking for the following columns.
City,Year,Congestion,Rank,worstDay/month,bestDay/month,worstDay Congestion,Best Day Congestion
The result for Dublin (https://www.tomtom.com/en_gb/traffic-index/page-data/dublin-traffic/page-data.json) would then be as below. Thanks again. 🙂
Hi, John343467 , the solution is really verbose as requested fields lie in different layers of hierarchy. you might want to try this solution,
let
Source = Json.Document(Web.Contents("https://www.tomtom.com/en_gb/traffic-index/page-data/stuttgart-traffic/page-data.json")),
tbl = Table.FromRecords({Source[result][data][citiesJson]}),
#"Removed Columns" = Table.SelectColumns(tbl, {"name"} & List.Select(Table.ColumnNames(tbl), each Text.Contains(_,"stats"))),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"name"}, "Year", "Record"),
#"Extracted Yr" = Table.TransformColumns(#"Unpivoted Other Columns", {{"Year", each Number.From(Text.Select(_,{"0".."9"})), type number}}),
#"Expanded Record" = Table.ExpandRecordColumn(#"Extracted Yr", "Record", {"rank", "congestion", "results"}, {"rank", "congestion", "results"}),
results = Table.TransformColumns(#"Expanded Record",
{
{"results",
each
[
worst = [worstDay],
best = [bestDay],
res = [
#"worst Day/Month" = Text.From(worst[day])&"/"&Text.From(worst[month]),
#"worst Congestion" = Number.Round(worst[congestion]),
#"best Day/Month" = Text.From(best[day])&"/"&Text.From(best[month]),
#"best Congestion" = Number.Round(best[congestion])
]
][res]
}
}
),
#"Expanded results" = Table.ExpandRecordColumn(results, "results", {"worst Day/Month", "worst Congestion", "best Day/Month", "best Congestion"}, {"worst Day/Month", "worst Congestion", "best Day/Month", "best Congestion"})
in
#"Expanded results"
- John3434675 years ago
Helper I
CNENFRNL Great, thank you. Just wondering how you worked that out? I was stuck on it for quite a while. Any good sources I could investigate? Thanks again.
- CNENFRNL5 years ago
Community Champion
Hi, John343467 , glad that the solution is of help.
When I began to learn M language, a guru suggested me spending time to gain a firm grasp of the very fundamental data containers in M, namely Record, List, Table. So I spent quite some hours gaining acquaintance of transformations among them by using functions, such as Table.FromColumns, Table.FromRows, Table.FromRecords, Record.ToList, etc. It seems now such efforts pay off for me. You might want to give it a shot.
Enjoy M language, enjoy Power Query!