Convert json List Record to Table value in PowerBI
Asked Answered
B

3

8

I'm totally new to Power BI so I'm tried different approached to convert my JSON file to a table but unsuccessful so far.

{
    "Family": [
        {
            "Father": "F1",
            "Age": 50,
            "Mother": "M1",
            "MAge": 49,
            "Children": [
                {
                    "Name": "C1"
                },
                {
                    "Name": "C2"
                }
            ]
        },
        {
            "Father": "F2",
            "Age": 55,
            "Mother": "M2",
            "MAge": 53,
            "Children": [
                {
                    "Name": "Cc1"
                },
                {
                    "Name": "Cc2"
                }
            ]
        }
    ]
}

I'm trying to convert this into the table below

Father    Age      Mother    MAge
F1         50        M1        49

F2         55        M2        53

I tried like convert table and transpose which is not working I always get an error like

Expression.Error: We cannot convert a value of type Record to type

Berserk answered 3/3, 2019 at 7:30 Comment(0)
B
5

I'm missing the step of ExpandRecordColumn Function. After I put that in my Query. It's worked.

let
    Source = Json.Document(File.Contents("C:\Users\hp\Desktop\File.JSON")),
    Family = Source[Family],
    #"Converted to Table" = Table.FromList(Family, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"Father", "Age", "Mother", "MAge", "Children"}, {"Father", "Age", "Mother", "MAge", "Children"})
in
    #"Expanded Column1"
Berserk answered 6/3, 2019 at 7:3 Comment(1)
I added an answer to show how you can do it step by step in UI. +1Prole
P
16

You can do it using only the web/desktop interface:

  1. Open Power BI Desktop

  2. Get DataMore ... → Select JSON from the list and click on Connect (You can also use WEB API or other sources which gives you JSON data)

  3. Choose the JSON file and open it.

  4. In the data panel, you see Family|List. Click on List link to add a navigation step.

  5. From Transform tab, click on To Table and from dialog click on OK.

  6. From the header of "Column1", click on Expand columns button expand columns to expand columns and from the menu, uncheck Use original column name as prefix and check the columns that you want and click on OK.

  7. You will see the columns and data in table format. You can change data type of columns by click on data type button change data type.

And finally you will have something like this:

power bi convert json data to table

Finally if you want to see the generated query, click on Advanced Editor button and see the code:

let
    Source = Json.Document(File.Contents("C:\Users\rag\Desktop\data.json")),
    Family = Source[Family],
    #"Converted to Table" = Table.FromList(Family, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"Father", "Age", "Mother", "MAge"}, {"Father", "Age", "Mother", "MAge"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1",{{"Age", type number}, {"MAge", type number}})
in
    #"Changed Type"

More information:

Prole answered 6/6, 2020 at 4:52 Comment(0)
B
5

I'm missing the step of ExpandRecordColumn Function. After I put that in my Query. It's worked.

let
    Source = Json.Document(File.Contents("C:\Users\hp\Desktop\File.JSON")),
    Family = Source[Family],
    #"Converted to Table" = Table.FromList(Family, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"Father", "Age", "Mother", "MAge", "Children"}, {"Father", "Age", "Mother", "MAge", "Children"})
in
    #"Expanded Column1"
Berserk answered 6/3, 2019 at 7:3 Comment(1)
I added an answer to show how you can do it step by step in UI. +1Prole
O
-2

I just followed this article to get data from a JSON file. When I clicked the Transform button, the Power Query engine automatically show me the final table. So I think Now Power bi(latest version 2.107. 683.0) has auto capability features to do this job for us. Thanks.

enter image description here

Ode answered 30/8, 2022 at 22:5 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Pulque

© 2022 - 2024 — McMap. All rights reserved.