Forum Discussion

ElliotP's avatar
ElliotP
Post Prodigy
9 years ago
Solved

Nested JSON and never end Records

Afternoon,

 

I have a json file (https://1drv.ms/u/s!At8Q-ZbRnAj8hkJLL1cyU4t_hoHC) which has many nested arrays and I'm unsure of how to extract all of the records into powerbi.

 

I've watch the guyinacube video (incredibly helpful), but I get to a point where I can't expand my columns any further yet I need the data inside that record (it's actually the data I want).

 

First step: https://gyazo.com/86e411d5de9d7f2f52813f6f66cb3bf9

Convert to table, easy it converts out to this.

 

Second Step: https://gyazo.com/546206f9a6b0459a4a09b2c865c3d902

Expand out, again comfortable. Expand again.

 

Third Step (issue): https://gyazo.com/ffe36ce86fa91f4ae0a435c524d026e5

The column on the far right is the colum which contains the data that i want, but I'm unable to retrieve the data from that column without individually clicking on the links.

 

Any ideas as to how I would be to produce rows or tables from this?

  • Here is the final result that you can follow

     

    let
        json= Json.Document(File.Contents("D:\Downloads\Xero Datapowerbiforum")),
        json_tab = Table.FromList(json, Splitter.SplitByNothing()),
        expand_1 = Table.ExpandRecordColumn(json_tab, "Column1", {"JournalID", "JournalDate", "JournalNumber", "CreatedDateUTC", "SourceID", "SourceType", "JournalLines"}),
        expand_2 = Table.ExpandRecordColumn(expand_1, "JournalLines", {"JournalLine"}),
        journal_line_transform = Table.TransformColumns(expand_2, {"JournalLine", each if _ is record then {_}  else _}),
        expand_3 = Table.ExpandListColumn(journal_line_transform, "JournalLine"),
        expand_4 = Table.ExpandRecordColumn(expand_3, "JournalLine", {"JournalLineID", "AccountID", "AccountCode", "AccountType", "AccountName", "Description", "NetAmount", "GrossAmount", "TaxAmount", "TaxType", "TaxName"}, {"JournalLineID", "AccountID", "AccountCode", "AccountType", "AccountName", "Description", "NetAmount", "GrossAmount", "TaxAmount", "TaxType", "TaxName"})
    in
        expand_4

     

10 Replies

  • ImkeF's avatar
    ImkeF
    Community Champion

    Yes, normally you would create a function that opens and transforms one (sample) record and apply this in for each cell by calling it from a new column.

    But this would only work, if all items have the same structure. This doesn't seem to be the case for your data here, as there are records and lists in them. So you would at least need two different functions and apply them using a conditional statement in the new column.

    Or do you know in advance that some of these rows will not be needed and can then filter them out before the expansion?

  • astoz's avatar
    astoz
    Frequent Visitor

    Hello, this post is really useful thanks, It helped me solving an issue I had

    I'd like to check my understanding of this piece of code in the solution:

    if _ is record then {_} else _}

     

    Am I right in assuming that here it says if current value is record then store it in an object else go to the next value? I'm asking cause I'm not yet entirely clear on what _ (underscore) means in Power Query