Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
yamaga
Frequent Visitor

Transform Json record into Table

Hi

 

i ave this Json dataset and I want to transform it into a table with columns indicated in the json: Prodcut_ID and PRICE

the values are int rows records:

 

 

 

{
    "columns": [
        {
            "name": "PRODUCT_ID",
            "type": "text"
        },
        {
            "name": "PRICE",
            "type": "float"
        }
    ],
    "rows": [
        [
            "A26",
            0.30447
        ],
        [
            "A158",
            1.0970999
        ],
        [
            "A18898",
            0.205
        ],
        [
            "A25177",
            19.4
        ],
        [
            "A265",
            130.95
        ]
    ]
}

 

 

 

My target output:

 

 

    PRODUCT_ID  |    PRICE    
---------------+---------------
A26             |0.30447        
A158            |1.0970999      
A18898          |0.205          
A25177          |19.4           
    

 

 

 

Thanks in advance

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Hello @yamaga ,
first you parse the JSON and then extract the values as follows:

let
    Query1 = "{#(cr)#(lf)    ""columns"": [#(cr)#(lf)        {#(cr)#(lf)            ""name"": ""PRODUCT_ID"",#(cr)#(lf)            ""type"": ""text""#(cr)#(lf)        },#(cr)#(lf)        {#(cr)#(lf)            ""name"": ""PRICE"",#(cr)#(lf)            ""type"": ""float""#(cr)#(lf)        }#(cr)#(lf)    ],#(cr)#(lf)    ""rows"": [#(cr)#(lf)        [#(cr)#(lf)            ""A26"",#(cr)#(lf)            0.30447#(cr)#(lf)        ],#(cr)#(lf)        [#(cr)#(lf)            ""A158"",#(cr)#(lf)            1.0970999#(cr)#(lf)        ],#(cr)#(lf)        [#(cr)#(lf)            ""A18898"",#(cr)#(lf)            0.205#(cr)#(lf)        ],#(cr)#(lf)        [#(cr)#(lf)            ""A25177"",#(cr)#(lf)            19.4#(cr)#(lf)        ],#(cr)#(lf)        [#(cr)#(lf)            ""A265"",#(cr)#(lf)            130.95#(cr)#(lf)        ]#(cr)#(lf)    ]#(cr)#(lf)}",
    #"Parsed JSON" = Json.Document(Query1),
    Custom1 = Table.FromRows(#"Parsed JSON"[rows], List.Transform(#"Parsed JSON"[columns], each _[name]))
in
    Custom1


If you copy this code into the advanced editor you will see that the first step contains your JSON (with some line break code) and then the relevant steps follow.


Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

2 REPLIES 2
yamaga
Frequent Visitor

Thanks @ImkeF for your help, it works 

ImkeF
Super User
Super User

Hello @yamaga ,
first you parse the JSON and then extract the values as follows:

let
    Query1 = "{#(cr)#(lf)    ""columns"": [#(cr)#(lf)        {#(cr)#(lf)            ""name"": ""PRODUCT_ID"",#(cr)#(lf)            ""type"": ""text""#(cr)#(lf)        },#(cr)#(lf)        {#(cr)#(lf)            ""name"": ""PRICE"",#(cr)#(lf)            ""type"": ""float""#(cr)#(lf)        }#(cr)#(lf)    ],#(cr)#(lf)    ""rows"": [#(cr)#(lf)        [#(cr)#(lf)            ""A26"",#(cr)#(lf)            0.30447#(cr)#(lf)        ],#(cr)#(lf)        [#(cr)#(lf)            ""A158"",#(cr)#(lf)            1.0970999#(cr)#(lf)        ],#(cr)#(lf)        [#(cr)#(lf)            ""A18898"",#(cr)#(lf)            0.205#(cr)#(lf)        ],#(cr)#(lf)        [#(cr)#(lf)            ""A25177"",#(cr)#(lf)            19.4#(cr)#(lf)        ],#(cr)#(lf)        [#(cr)#(lf)            ""A265"",#(cr)#(lf)            130.95#(cr)#(lf)        ]#(cr)#(lf)    ]#(cr)#(lf)}",
    #"Parsed JSON" = Json.Document(Query1),
    Custom1 = Table.FromRows(#"Parsed JSON"[rows], List.Transform(#"Parsed JSON"[columns], each _[name]))
in
    Custom1


If you copy this code into the advanced editor you will see that the first step contains your JSON (with some line break code) and then the relevant steps follow.


Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors