Forum Discussion

Naesevol's avatar
Naesevol
Frequent Visitor
7 years ago
Solved

Parse Dynamic JSON from SQL Column

I've posted this earlier to Desktop, but apparently that post didn't take, so forgive me if this shows up twice!   My goal is to parse a JSON string column that is being served up to power bi via a...
  • ImkeF's avatar
    ImkeF
    7 years ago

    Hi Naesevol 

    this gets us back to my original solution where I assumed that there must probably more than one JSON object involved. Im just borrowing the last lines of code and combine them with your scenario:

     

    let
        Source = Sql.Database("mysqlserver", "mysqldb", [Query="SELECT * FROM mySQLView#(lf)"]),
        #"Filtered Rows" = Table.SelectRows(Source, each ([EvalResults] <> null)),
        #"Sorted Rows1" = Table.Sort(#"Filtered Rows",{{"AcquireTime", Order.Descending}}),
        #"Parsed JSON" = Table.TransformColumns(#"Sorted Rows1",{{"EvalResults", Json.Document}}),
        #"Expanded EvalResults" = Table.ExpandRecordColumn(#"Parsed JSON", "EvalResults", {"values"}, {"EvalResults.values"}),
        ListOfRecordsToTable = Table.AddColumn(#"Expanded EvalResults", "Custom", each Record.ToTable(Record.Combine([EvalResults.values]))),
        #"Expanded Custom" = Table.ExpandTableColumn(ListOfRecordsToTable, "Custom", {"Name", "Value"}, {"Name", "Value"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"values"}),
        #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Name]), "Name", "Value")
    in
        #"Pivoted Column"

    Of course here could be typos or name mismatches, as I couldn't test your code.

    But the important thing is NOT to expand the list column (as this will cause the dups), but instead add the column "Custom" instead.