简体   繁体   中英

Convert record with Power Query and JSON

I'm using Power Query on Excel 2013 to convert an huge JSON file (more than 100Mb) to plain excel sheet.

All the fields except one are converted correctly but there is one specific field that is recognized as record. All other fields have a fixed text value or values separated by comma, so the conversion is pretty easy, this field inside has a JSON record structure so "Field" : "Value".

This is an extract of the file:

    {
    "idTrad": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "images": {
        "1": "SE1.JPG",
        "2": "SE2.JPG"
    },
    "date": "2018-09-22",
    "category": "MD",
    "value": "Original text",
    "language": "IT",
    "contexts": [
        ""
    ],
    "label": "Translated text",
    "variantes": "1,23,45,23,32,232,2315,23131",
    "theme": [
        "XX_XXX"
    ]
}

The problematic field is "images" because it's recognized as a record, in the resulting table I have this situation:

[1]: https://i.stack.imgur.com/EnHow.png

My query so far is:

    let

    Source = Json.Document(File.Contents("filename.json")),

 #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),

    #"Column1 développé" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"value", "contexts", "theme", "variantes", "category", "label", "language", "idTrad","images", "date"}, {"Column1.value", "Column1.contexts", "Column1.theme", "Column1.variantes", "Column1.category", "Column1.label", "Column1.language", "Column1.idTrad","Column1.images", "Column1.date"}),

    #"Valeurs extraites" = Table.TransformColumns(#"Column1 développé", {"Column1.contexts", each Text.Combine(List.Transform(_, Text.From), ","), type text}),

   #"Valeurs extraites1" = Table.TransformColumns(#"Valeurs extraites", {"Column1.theme", each Text.Combine(List.Transform(_, Text.From), ","), type text})


in    

    #"Valeurs extraites1"

I would like to have in the images field a text rappresentation of the record so something like "1: SE1.JPG, 2: SE2.JPG", any ideas?

Sure, you can even do it in one step! If you convert a record to a table (Record.ToTable) it will create a table where the names of the fields in your record are in a column called "Name" and the values are in a column called "Value". This way you get your "1", "2", etc from the json file. From there you can just combine the columns into the text you want and convert and combine a list like you did in the rest of your columns.

= Table.TransformColumns(#"Valeurs extraites1",{"Column1.images", 
each 
Text.Combine(
 Table.ToList(
  Table.CombineColumns(
   Record.ToTable(_)
  ,{"Name", "Value"},Combiner.CombineTextByDelimiter(": ", QuoteStyle.None),"Merged")
 )
, ", ")
})

I wouldn't think Record.ToTable localizes it's column naming , but maybe test it with just converting the record to a table first to see what it does.

Table.TransformColumns(#"Valeurs extraites1",{"Column1.images",each Record.ToTable(_)})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM