Forum Discussion
Convert Partial JSON to Excel Columns?
- 2 years ago
let Source = Json.Document(" [ { ""field1"": ""blah 1"", ""field2"": ""blah 2"", ""rules"": [ { ""anotherfield1"": ""blah 3"", ""anotherfield2"": ""blah 4"", ""Pattern"": { ""a"": ""blah 5"", ""b"": ""blah 6"" } }, { ""anotherfield1"": ""blah 7"", ""anotherfield2"": ""blah 8"", ""Pattern"": { ""a"": ""blah 9"", ""b"": ""blah 10"" } } ] } ] "), #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error), #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"field1", "field2", "rules"}, {"field1", "field2", "rules"}), #"Replaced Value" = Table.ReplaceValue(#"Expanded Column1",each [rules],each Text.FromBinary(Json.FromValue([rules])),Replacer.ReplaceValue,{"rules"}) in #"Replaced Value"How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.
Please provide meaningful sample data (at a minimum a functioning JSON) and indicate the expected result from that sample data.
- You can put whatever meaningful sample data is desired. I changed it as is sensitive and not needed to address the question.
- I had given an example of what the json structure generally looks like, but looks like I left out a " and , that broke it in the process. This is a functioning example that also excludes the ...(etc)... parts to indicate additional data fields/records are actually present:
[
{
"field1": "blah 1",
"field2": "blah 2",
"rules": [
{
"anotherfield1": "blah 3",
"anotherfield2": "blah 4",
"Pattern": {
"a": "blah 5",
"b": "blah 6"
}
},
{
"anotherfield1": "blah 7",
"anotherfield2": "blah 8",
"Pattern": {
"a": "blah 9",
"b": "blah 10"
}
}
]
}
]- I had already given the expected output. If you weren't sure what the rules column is supposed to look like, then this is what it would look like:
- lbendlin2 years ago
Super User
let Source = Json.Document(" [ { ""field1"": ""blah 1"", ""field2"": ""blah 2"", ""rules"": [ { ""anotherfield1"": ""blah 3"", ""anotherfield2"": ""blah 4"", ""Pattern"": { ""a"": ""blah 5"", ""b"": ""blah 6"" } }, { ""anotherfield1"": ""blah 7"", ""anotherfield2"": ""blah 8"", ""Pattern"": { ""a"": ""blah 9"", ""b"": ""blah 10"" } } ] } ] "), #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error), #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"field1", "field2", "rules"}, {"field1", "field2", "rules"}), #"Replaced Value" = Table.ReplaceValue(#"Expanded Column1",each [rules],each Text.FromBinary(Json.FromValue([rules])),Replacer.ReplaceValue,{"rules"}) in #"Replaced Value"How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.
- ExcelPWRQ012 years agoNew Member
Looks like this is the part that I didn't know how to do before:
= Table.ReplaceValue(#"Expanded Column1",each [rules],each Text.FromBinary(Json.FromValue([rules])),Replacer.ReplaceValue,{"rules"})
This is great for extracting the json content at that data field. Is there any additional syntax available for the cell output to show 'pretty' json and not condensed like my example output?
- lbendlin2 years ago
Super User
You could go cute on it and inject line feeds but that's a bit much, no?