Forum Discussion
Tranform JSON to table
- 7 years ago
For those interested, here is what i did in the end.
Using 'search and replace' I changed the JSON. With various steps I moved the value i want to split into rows on, into the OBJECT, so that I end up with an array of objects. This allows me to "split to rows", ending up with individual OBJECTS, and then i extract the data i need from the OBJECT.
let Source = Sql.Databases("XXXX.database.windows.net"), XXXX = Source{[Name="XXXX"]}[Data], dbo_table = XXXXX{[Schema="dbo",Item="dbo_table"]}[Data], #"Added Prefix" = Table.TransformColumns(dbo_table, {{"zones", each "[" & _, type text}}), #"Added Suffix" = Table.TransformColumns(#"Added Prefix", {{"zones", each _ & "]", type text}}), #"Replaced Value" = Table.ReplaceValue(#"Added Suffix","},","}},{",Replacer.ReplaceText,{"zones"}), #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","{""#","{""zonerid"":""#",Replacer.ReplaceText,{"zones"}), #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1",":{""s",",""s",Replacer.ReplaceText,{"zones"}), #"Replaced Value3" = Table.ReplaceValue(#"Replaced Value2","}}","}",Replacer.ReplaceText,{"zones"}), #"Parsed JSON" = Table.TransformColumns(#"Replaced Value3",{{"zones", Json.Document}}), #"Expanded zones" = Table.ExpandListColumn(#"Parsed JSON", "zones"), #"Expanded zones1" = Table.ExpandRecordColumn(#"Expanded zones", "zones", {"zonerid"}, {"zones.zonerid"}) in #"Expanded zones1"
Thanks a lot Lin
I have this problem with different columns in the dataset also, so though the code looks great, it's gonna be too much of a hassle to convert etc. I think our solution lies in writing the data differently.
Thanks for your effort, much appreciated.
Roelf
For those interested, here is what i did in the end.
Using 'search and replace' I changed the JSON. With various steps I moved the value i want to split into rows on, into the OBJECT, so that I end up with an array of objects. This allows me to "split to rows", ending up with individual OBJECTS, and then i extract the data i need from the OBJECT.
let
Source = Sql.Databases("XXXX.database.windows.net"),
XXXX = Source{[Name="XXXX"]}[Data],
dbo_table = XXXXX{[Schema="dbo",Item="dbo_table"]}[Data],
#"Added Prefix" = Table.TransformColumns(dbo_table, {{"zones", each "[" & _, type text}}),
#"Added Suffix" = Table.TransformColumns(#"Added Prefix", {{"zones", each _ & "]", type text}}),
#"Replaced Value" = Table.ReplaceValue(#"Added Suffix","},","}},{",Replacer.ReplaceText,{"zones"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","{""#","{""zonerid"":""#",Replacer.ReplaceText,{"zones"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1",":{""s",",""s",Replacer.ReplaceText,{"zones"}),
#"Replaced Value3" = Table.ReplaceValue(#"Replaced Value2","}}","}",Replacer.ReplaceText,{"zones"}),
#"Parsed JSON" = Table.TransformColumns(#"Replaced Value3",{{"zones", Json.Document}}),
#"Expanded zones" = Table.ExpandListColumn(#"Parsed JSON", "zones"),
#"Expanded zones1" = Table.ExpandRecordColumn(#"Expanded zones", "zones", {"zonerid"}, {"zones.zonerid"})
in
#"Expanded zones1"