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
DanielV91
Frequent Visitor

Can I achieve this with JSON

I have the following JSON but with more products

{
  "products": [
    {
      "product": "Tank Toy",
      "progress": "done",
      "components": [
        {
          "self": "https://dummy.com/1",
          "id": "1",
          "name": "plastic"
        },
        {
          "self": "https://dummy.com/2",
          "id": "2",
          "name": "metal"
        },
        {
          "self": "https://dummy.com/5",
          "id": "5",
          "name": "polyurethane"
        }
      ],
      "id": "100"
    },
    {
      "product": "Car Toy",
      "progress": "in progress",
      "components": [
        {
          "self": "https://dummy.com/6",
          "id": "6",
          "name": "polycarbonate"
        },
        {
          "self": "https://dummy.com/12",
          "id": "12",
          "name": "aluminium"
        }
      ],
      "id": "23"
    },
    {
      "product": "Doll Toy",
      "progress": "done",
      "components": [
        {
          "self": "https://dummy.com/11",
          "id": "6",
          "name": "Polystyrene"
        }
      ],
      "id": "40"
    }
  ]
}

I am interested into generating a table like this

productprogresscomponentsid
Tank Toydoneplastic,metal,polyurethane100
Car Toyin progresspolycarbonate,aluminium23
Doll Toydonepolystyrene40

 

As you can see the only thing that is bugging me is how can I join all the components name into a single cell. I am only interested in the names in the components array. It is bugging me that a product can have multiple components. I couldn't make it work and I want to know if it is possible. 

If somebody has an idea I would gladly appreciate

3 ACCEPTED SOLUTIONS
DanielV91
Frequent Visitor

Eric_Zhang
Employee
Employee

@DanielV91

I'd suggest extract the JSON in below format, as it is more normalized.

Capture.PNG

let
    Source = Json.Document("{
  ""products"": [
    {
      ""product"": ""Tank Toy"",
      ""progress"": ""done"",
      ""components"": [
        {
          ""self"": ""https://dummy.com/1"",
          ""id"": ""1"",
          ""name"": ""plastic""
        },
        {
          ""self"": ""https://dummy.com/2"",
          ""id"": ""2"",
          ""name"": ""metal""
        },
        {
          ""self"": ""https://dummy.com/5"",
          ""id"": ""5"",
          ""name"": ""polyurethane""
        }
      ],
      ""id"": ""100""
    },
    {
      ""product"": ""Car Toy"",
      ""progress"": ""in progress"",
      ""components"": [
        {
          ""self"": ""https://dummy.com/6"",
          ""id"": ""6"",
          ""name"": ""polycarbonate""
        },
        {
          ""self"": ""https://dummy.com/12"",
          ""id"": ""12"",
          ""name"": ""aluminium""
        }
      ],
      ""id"": ""23""
    },
    {
      ""product"": ""Doll Toy"",
      ""progress"": ""done"",
      ""components"": [
        {
          ""self"": ""https://dummy.com/11"",
          ""id"": ""6"",
          ""name"": ""Polystyrene""
        }
      ],
      ""id"": ""40""
    }
  ]
}"),
    products = Source[products],
    #"Converted to Table" = Table.FromList(products, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"product", "progress", "components", "id"}, {"Column1.product", "Column1.progress", "Column1.components", "Column1.id"}),
    #"Expanded Column1.components" = Table.ExpandListColumn(#"Expanded Column1", "Column1.components"),
    #"Expanded Column1.components1" = Table.ExpandRecordColumn(#"Expanded Column1.components", "Column1.components", {"name"}, {"Column1.components.name"})
in
    #"Expanded Column1.components1"

For the case in your Post.

Capture.PNG

 

let
    Source = Json.Document("{
  ""products"": [
    {
      ""product"": ""Tank Toy"",
      ""progress"": ""done"",
      ""components"": [
        {
          ""self"": ""https://dummy.com/1"",
          ""id"": ""1"",
          ""name"": ""plastic""
        },
        {
          ""self"": ""https://dummy.com/2"",
          ""id"": ""2"",
          ""name"": ""metal""
        },
        {
          ""self"": ""https://dummy.com/5"",
          ""id"": ""5"",
          ""name"": ""polyurethane""
        }
      ],
      ""id"": ""100""
    },
    {
      ""product"": ""Car Toy"",
      ""progress"": ""in progress"",
      ""components"": [
        {
          ""self"": ""https://dummy.com/6"",
          ""id"": ""6"",
          ""name"": ""polycarbonate""
        },
        {
          ""self"": ""https://dummy.com/12"",
          ""id"": ""12"",
          ""name"": ""aluminium""
        }
      ],
      ""id"": ""23""
    },
    {
      ""product"": ""Doll Toy"",
      ""progress"": ""done"",
      ""components"": [
        {
          ""self"": ""https://dummy.com/11"",
          ""id"": ""6"",
          ""name"": ""Polystyrene""
        }
      ],
      ""id"": ""40""
    }
  ]
}"),
    products = Source[products],
    #"Converted to Table" = Table.FromList(products, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"product", "progress", "components", "id"}, {"Column1.product", "Column1.progress", "Column1.components", "Column1.id"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Column1", "Custom", each Text.Combine(Table.ToList(Table.SelectColumns(Table.FromList([Column1.components],Record.FieldValues, {"self", "id", "Name"}),"Name")),",")),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each Text.Combine([Custom],each Text.From(_))),
    #"Column1 components" = #"Added Custom1"{0}[Column1.components]
in
    #"Column1 components"

 

View solution in original post

Thank you very much for helping @Eric_Zhang Sorry I couldn't reply faster.

View solution in original post

3 REPLIES 3
Eric_Zhang
Employee
Employee

@DanielV91

I'd suggest extract the JSON in below format, as it is more normalized.

Capture.PNG

let
    Source = Json.Document("{
  ""products"": [
    {
      ""product"": ""Tank Toy"",
      ""progress"": ""done"",
      ""components"": [
        {
          ""self"": ""https://dummy.com/1"",
          ""id"": ""1"",
          ""name"": ""plastic""
        },
        {
          ""self"": ""https://dummy.com/2"",
          ""id"": ""2"",
          ""name"": ""metal""
        },
        {
          ""self"": ""https://dummy.com/5"",
          ""id"": ""5"",
          ""name"": ""polyurethane""
        }
      ],
      ""id"": ""100""
    },
    {
      ""product"": ""Car Toy"",
      ""progress"": ""in progress"",
      ""components"": [
        {
          ""self"": ""https://dummy.com/6"",
          ""id"": ""6"",
          ""name"": ""polycarbonate""
        },
        {
          ""self"": ""https://dummy.com/12"",
          ""id"": ""12"",
          ""name"": ""aluminium""
        }
      ],
      ""id"": ""23""
    },
    {
      ""product"": ""Doll Toy"",
      ""progress"": ""done"",
      ""components"": [
        {
          ""self"": ""https://dummy.com/11"",
          ""id"": ""6"",
          ""name"": ""Polystyrene""
        }
      ],
      ""id"": ""40""
    }
  ]
}"),
    products = Source[products],
    #"Converted to Table" = Table.FromList(products, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"product", "progress", "components", "id"}, {"Column1.product", "Column1.progress", "Column1.components", "Column1.id"}),
    #"Expanded Column1.components" = Table.ExpandListColumn(#"Expanded Column1", "Column1.components"),
    #"Expanded Column1.components1" = Table.ExpandRecordColumn(#"Expanded Column1.components", "Column1.components", {"name"}, {"Column1.components.name"})
in
    #"Expanded Column1.components1"

For the case in your Post.

Capture.PNG

 

let
    Source = Json.Document("{
  ""products"": [
    {
      ""product"": ""Tank Toy"",
      ""progress"": ""done"",
      ""components"": [
        {
          ""self"": ""https://dummy.com/1"",
          ""id"": ""1"",
          ""name"": ""plastic""
        },
        {
          ""self"": ""https://dummy.com/2"",
          ""id"": ""2"",
          ""name"": ""metal""
        },
        {
          ""self"": ""https://dummy.com/5"",
          ""id"": ""5"",
          ""name"": ""polyurethane""
        }
      ],
      ""id"": ""100""
    },
    {
      ""product"": ""Car Toy"",
      ""progress"": ""in progress"",
      ""components"": [
        {
          ""self"": ""https://dummy.com/6"",
          ""id"": ""6"",
          ""name"": ""polycarbonate""
        },
        {
          ""self"": ""https://dummy.com/12"",
          ""id"": ""12"",
          ""name"": ""aluminium""
        }
      ],
      ""id"": ""23""
    },
    {
      ""product"": ""Doll Toy"",
      ""progress"": ""done"",
      ""components"": [
        {
          ""self"": ""https://dummy.com/11"",
          ""id"": ""6"",
          ""name"": ""Polystyrene""
        }
      ],
      ""id"": ""40""
    }
  ]
}"),
    products = Source[products],
    #"Converted to Table" = Table.FromList(products, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"product", "progress", "components", "id"}, {"Column1.product", "Column1.progress", "Column1.components", "Column1.id"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Column1", "Custom", each Text.Combine(Table.ToList(Table.SelectColumns(Table.FromList([Column1.components],Record.FieldValues, {"self", "id", "Name"}),"Name")),",")),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each Text.Combine([Custom],each Text.From(_))),
    #"Column1 components" = #"Added Custom1"{0}[Column1.components]
in
    #"Column1 components"

 

Thank you very much for helping @Eric_Zhang Sorry I couldn't reply faster.
DanielV91
Frequent Visitor

Any advise?

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.