Search code examples
powerbipowerquerypowerbi-desktopdata-cleaningm

Power Query: Structured record while converting to JSON


I have a power query record as follows:

Data = [
        tracker = {
            [
                foo = {
                        [
                            field_a= "Something",
                            field_b = "data2"
                        ],
                        [
                            field_a= "Something",
                            field_c = "data2"
                        ]
                },
                bar = "Data"
            ],
    
            [
                foo = {
                        [
                            field_c= "Something",
                            field_b = "data2"
                        ],
                        [
                            field_a= "Something",
                            field_c = "data2"
                        ]
                },
                bar = "Data2"
            ]
        } 
    ]

When I convert it into a JSON, I obtain this:

{
  "tracker": [
    {
      "foo": [
        {
          "field_a": "Something",
          "field_b": "data2"
        },
        {
          "field_a": "Something",
          "field_c": "data2"
        }
      ],
      "bar": "Data"
    },
    {
      "foo": [
        {
          "field_c": "Something",
          "field_b": "data2"
        },
        {
          "field_a": "Something",
          "field_c": "data2"
        }
      ],
      "bar": "Data2"
    }
  ]
}

I need the foo array to have structured objects within it, in the sense that each object should have field_a, field_b, field_c in it. Example:

foo = {
        [
            field_c= "Something",
            field_b = "data2",
            field_a = null
        ],
        [
            field_a= "Something",
            field_c = "data2".
            field_b = null
        ]
    }

I tried using

List.Transform(x, each Record.TransformFields(_, {
        { "field_a", Text.Trim },
        { "field_b", Text.Trim },
        { "field_c", Text.Trim }
    }, MissingField.UseNull))

however, I am losing reference to the bar field in the final output. Any help would be appreciated.


Solution

  • This seems to work for me.

    let
        Data = [
            tracker = {
                [
                    foo = {
                            [
                                field_a= "Something",
                                field_b = "data2"
                            ],
                            [
                                field_a= "Something",
                                field_c = "data2"
                            ]
                    },
                    bar = "Data"
                ],
        
                [
                    foo = {
                            [
                                field_c= "Something",
                                field_b = "data2"
                            ],
                            [
                                field_a= "Something",
                                field_c = "data2"
                            ]
                    },
                    bar = "Data2"
                ]
            } 
        ],
        #"Converted to Table" = Record.ToTable(Data),
        #"Added Custom" = Table.AddColumn(#"Converted to Table", "Custom", 
        each let 
            a = [Value],
            b = List.Transform(a, each Record.TransformFields(_, {
                { "foo", (x)=> List.Transform(x, (y)=> Record.TransformFields(y, {
                { "field_a", Text.Trim },
                { "field_b", Text.Trim },
                { "field_c", Text.Trim }
            }, MissingField.UseNull) )}
                
            }))
        in b)
    in
        #"Added Custom"