Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Parsing JSON column to different columns

I have one column in the table Details with name "Response" in metadata format- like below

example record in responses column:
1.  [{"Question":"Number of controls","QuestionOrder":1,"Response":"15"},{"Question":"Number of controls already implemented","QuestionOrder":2,"Response":"implemented"},{"Question":"Is the considered material for the implementation of the specific controls ","QuestionOrder":3,"Response":"8/20/2024"},{"Question":"Entity HM Code","QuestionOrder":4,"Response":"34"},{"Question":"applicability has been assessed","QuestionOrder":5,"Response":"MONDAY"}]

2. [{"Question":"Number of controls","QuestionOrder":1,"Response":"46"},{"Question":"Number of controls already implemented","QuestionOrder":2,"Response":"Sept"},{"Question":"Is the  considered material for the implementation of the specific controls ","QuestionOrder":3,"Response":"9/20/2024"},{"Question":"Entity HM Code","QuestionOrder":4,"Response":"6"}]

From Above I need to get few columns in power bi,

1.Number of  controls
2.Number of controls already implemented 
3.Is the considered material for the implementation of the specific controls 
4.Entity HM Code
5. applicability has assessed

I need Output as like below, need mentioned above 5 new columns in pbi:

 

HM CODENumber of controlsNumber of controls already implemented Is the considered material for the implementation of the specific controls Entity HM Codeapplicability  has been assessed
31915implemented8/20/202434MONDAY
31846Sept9/20/20246 

 

  • Hi Anonymous ,

     

    try this (with the help of chatgpt):

    let
        // or path to a json file
        Source = 
        "  
        [
            [
                {""Question"":""Number of controls"",""QuestionOrder"":1,""Response"":""15""},
                {""Question"":""Number of controls already implemented"",""QuestionOrder"":2,""Response"":""implemented""},
                {""Question"":""Is the considered material for the implementation of the specific controls"",""QuestionOrder"":3,""Response"":""8/20/2024""},
                {""Question"":""Entity HM Code"",""QuestionOrder"":4,""Response"":""34""},
                {""Question"":""applicability has been assessed"",""QuestionOrder"":5,""Response"":""MONDAY""}
            ],
            [
                {""Question"":""Number of controls"",""QuestionOrder"":1,""Response"":""46""},
                {""Question"":""Number of controls already implemented"",""QuestionOrder"":2,""Response"":""Sept""},
                {""Question"":""Is the considered material for the implementation of the specific controls"",""QuestionOrder"":3,""Response"":""9/20/2024""},
                {""Question"":""Entity HM Code"",""QuestionOrder"":4,""Response"":""6""}
            ]
        ]",
        
        // Parse JSON text into a list of lists
        ParsedJson = Json.Document(Text.FromBinary(Text.ToBinary(Source))),
        
        // Add an index to identify each response set
        IndexedData = Table.AddIndexColumn(Table.FromList(ParsedJson, Splitter.SplitByNothing(), {"Responses"}), "ResponseSet", 1, 1, Int64.Type),
        
        // Replace nulls with empty lists before expanding
        EnsureList = Table.TransformColumns(IndexedData, {"Responses", each if _ = null then {} else _}),
        
        // Expand the inner lists into rows
        ExpandedData = Table.ExpandListColumn(EnsureList, "Responses"),
        // Expand records into Question and Response columns
        ExpandedRecords = Table.ExpandRecordColumn(ExpandedData, "Responses", {"Question", "Response"}, {"Question", "Response"}),
        
        // Pivot the table to create columns for each question
        PivotedTable = Table.Pivot(
            ExpandedRecords,
            List.Distinct(ExpandedRecords[Question]),
            "Question",
            "Response"
        ),
        #"Removed Columns" = Table.RemoveColumns(PivotedTable,{"ResponseSet"})
    in
        #"Removed Columns"

     

     

1 Reply

  • Hi Anonymous ,

     

    try this (with the help of chatgpt):

    let
        // or path to a json file
        Source = 
        "  
        [
            [
                {""Question"":""Number of controls"",""QuestionOrder"":1,""Response"":""15""},
                {""Question"":""Number of controls already implemented"",""QuestionOrder"":2,""Response"":""implemented""},
                {""Question"":""Is the considered material for the implementation of the specific controls"",""QuestionOrder"":3,""Response"":""8/20/2024""},
                {""Question"":""Entity HM Code"",""QuestionOrder"":4,""Response"":""34""},
                {""Question"":""applicability has been assessed"",""QuestionOrder"":5,""Response"":""MONDAY""}
            ],
            [
                {""Question"":""Number of controls"",""QuestionOrder"":1,""Response"":""46""},
                {""Question"":""Number of controls already implemented"",""QuestionOrder"":2,""Response"":""Sept""},
                {""Question"":""Is the considered material for the implementation of the specific controls"",""QuestionOrder"":3,""Response"":""9/20/2024""},
                {""Question"":""Entity HM Code"",""QuestionOrder"":4,""Response"":""6""}
            ]
        ]",
        
        // Parse JSON text into a list of lists
        ParsedJson = Json.Document(Text.FromBinary(Text.ToBinary(Source))),
        
        // Add an index to identify each response set
        IndexedData = Table.AddIndexColumn(Table.FromList(ParsedJson, Splitter.SplitByNothing(), {"Responses"}), "ResponseSet", 1, 1, Int64.Type),
        
        // Replace nulls with empty lists before expanding
        EnsureList = Table.TransformColumns(IndexedData, {"Responses", each if _ = null then {} else _}),
        
        // Expand the inner lists into rows
        ExpandedData = Table.ExpandListColumn(EnsureList, "Responses"),
        // Expand records into Question and Response columns
        ExpandedRecords = Table.ExpandRecordColumn(ExpandedData, "Responses", {"Question", "Response"}, {"Question", "Response"}),
        
        // Pivot the table to create columns for each question
        PivotedTable = Table.Pivot(
            ExpandedRecords,
            List.Distinct(ExpandedRecords[Question]),
            "Question",
            "Response"
        ),
        #"Removed Columns" = Table.RemoveColumns(PivotedTable,{"ResponseSet"})
    in
        #"Removed Columns"