2015-11-26 65 views
3

我想要轉換(我是一個電源查詢轉換新手)json數據。我們的目標是合併和轉換一些json文件來創建報告。 json文件由我們的Web應用程序公開的不同Feed提供。作爲學習的一部分,我們嘗試轉換單個json文件。轉換json與電力查詢(在單個列中混合列表和記錄)

但場在我們的JSON數據的一個是爲通過功率查詢作爲記錄和列表類型的混合承認(在功率查詢該列中的一個顯示列表和記錄的混合)。

在這種情況下,我無法獲取這些訂單項的值。

screenshot for power query

任何解決這個問題的指針將是非常有益的

在此先感謝

請找到樣本JSON:

{ 
    "@timestamp": "27-11-2015 21:31:30 PM", "thingstodo": [{ 
    "propCode": "foo1" 
}, 
{ 
    "propCode": "foo2" 
}, 
{ 
    "propCode": "foo3", 
    "hours": [{ 
     "day": "Monday-Thursday", 
     "time": "2:00PM - 1:00AM" 
    }, 
    { 
     "day": "Friday-Sunday", 
     "time": "10:00AM - 2:00AM" 
    }] 
}, 
{ 

    "propCode": "foo4" 
}, 
{ 


    "propCode": "foo5", 
    "hours": [{ 
     "day": "Daily", 
     "time": "24 Hours" 
    }] 
}, 
{ 


    "propCode": "foo6", 
    "hours": [{ 
     "day": "10am - 4am", 
     "time": "bar & lounge area" 
    }, 
    { 
     "day": "12pm - 4am", 
     "time": "gaming area" 
    }] 
}, 
{ 
    "propCode": "foo7", 


    "hours": [{ 
     "day": "Daily", 
     "time": "11:00AM - 1:00AM" 
    }, 
    { 
     "day": "Happy Hour Daily", 
     "time": "4:00PM - 6:00PM" 
    }] 
}, 
{ 
    "propCode": "foo8" 
}, 
{ 
    "propCode": "foo9" 


}, 
{ 
    "propCode": "foo10", 
    "hours": [{ 
     "day": "Sun-Thu", 
     "time": "10:00AM - 11:00PM" 
    }, 
    { 
     "day": "Fri & Sat", 
     "time": "10:00AM - 12:00AM" 
    }] 
}, 
{ 
    "propCode": "foo11", 
    "name": "Atlantic City Deals and Packages", 
    "brand": "harrahs" 
}, 
{ 
    "propCode": "foo12" 
}, 
{ 


    "propCode": "foo13", 
    "hours": [{ 
     "day": "Sun-Thu", 
     "time": "11:00AM - 1:00AM" 
    }, 
    { 
     "day": "Fri-Sat", 
     "time": "11:00AM - 2:00AM" 
    }] 
}, 
{ 
    "propCode": "foo8", 


    "hours": [{ 
     "day": "Daily", 
     "time": "11:00AM - 2:00AM" 
    }] 
}, 
{ 
    "propCode": "foo12" 


}, 
{ 
    "propCode": "foo14" 
}, 
{ 


    "propCode": "foo14", 
    "hours": [{ 
     "day": "Daily", 
     "time": "6:00AM - 6:00PM" 
    }] 
}, 
{ 


    "propCode": "foo12", 
    "hours": [{ 
     "day": "Sunday-Thursday", 
     "time": "8:00AM - 6:00PM" 
    }, 
    { 
     "day": "Friday-Saturday", 
     "time": "8:00AM - 7:00PM<br>" 
    }, 
    { 
     "day": "<br>Adult Swim (21+)<br> Sunday-Thursday", 
     "time": "5:00PM - 6:00PM" 
    }, 
    { 
     "day": "Friday-Saturday", 
     "time": "5:00PM - 7:00PM" 
    }] 
}, 
{ 
    "propCode": "foo15", 


    "hours": [{ 
     "day": "Mon-Thu ", 
     "time": "9:00AM - 9:30PM
" 
    }, 
    { 
     "day": "Fri-Sun ", 
     "time": "9:00AM - Midnight" 
    }] 
}, 
{ 


    "propCode": "foo16", 
    "hours": [{ 
     "day": "Sun - Thurs:", 
     "time": "9:00AM - 5:00PM" 
    }, 
    { 
     "day": "Fri - Sat:", 
     "time": "9:00AM - 6:00PM" 
    }, 
    { 
     "day": "Slide", 
     "time": "Closed for the season" 
    }] 
}, 
{ 
    "propCode": "foo17", 


    "hours": [{ 
     "day": "Friday", 
     "time": "10:30PM " 
    }, 
    { 
     "day": "Saturday", 
     "time": "10:30PM " 
    }] 
}, 
{ 


    "propCode": "foo6" 
}, 
{ 
    "propCode": "foo14", 

    "hours": [{ 
     "day": "Sunday - Friday", 
     "time": "9:00AM - 5:00PM" 
    }, 
    { 
     "day": "Saturday", 
     "time": "9:00AM - 6:00PM" 
    }] 
}, 
{ 


    "propCode": "foo14", 
    "hours": [{ 
     "day": "Closed for ", 
     "time": "Winter Season" 
    }] 
}, 
{ 


    "propCode": "foo12", 
    "hours": [["CLOSED"]] 
}, 
{ 
    "propCode": "foo18", 


    "hours": [{ 
     "day": "Box Office Tue-Sat", 
     "time": "12:00PM - 8:00PM" 
    }] 
}, 
{ 
    "propCode": "foo19" 
}, 
{ 


    "propCode": "foo20", 
    "hours": [{ 
     "day": "Monday - Thursday", 
     "time": "<br>12:00PM - 3:00AM" 
    }, 
    { 
     "day": "Friday - Sunday", 
     "time": "<br>10:00AM - 3:00AM" 
    }, 
    { 
     "day": "Live Music", 
     "time": ", 6:00PM - 10:00PM" 
    }] 
}, 
{ 


    "propCode": "foo6", 
    "hours": [{ 
     "day": "Sunday:", 
     "time": "5:00PM - 1:00AM " 
    }, 
    { 
     "day": "Monday - Thursday:", 
     "time": "5:00PM - 1:00AM " 
    }, 
    { 
     "day": "Friday:", 
     "time": "5:00PM - 2:00AM " 
    }, 
    { 
     "day": "Saturday:", 
     "time": "5:00PM - 1:00AM " 
    }] 
}, 
{ 


    "propCode": "foo16", 
    "hours": [{ 
     "day": "Daily", 
     "time": "24 Hours" 
    }] 
}, 
{ 
    "propCode": "foo21", 


    "hours": [{ 
     "day": "Mon-Sat", 
     "time": "10:00AM - 2:00AM" 
    }, 
    { 
     "day": "Sun", 
     "time": "12:00PM - 2:00AM" 
    }] 
}, 
{ 
    "propCode": "foo10" 
}, 
{ 
    "propCode": "foo23", 
    "hours": [{ 
     "day": "Open Daily <br>(Seasonally)", 
     "time": "11:00AM - 6:00PM" 
    }] 
}, 
{ 
    "propCode": "foo22", 
    "hours": [{ 
     "day": "Daily", 
     "time": "11:00AM - 3:00AM" 
    }] 
}, 

    { 
    "propCode": "foo23", 

    "hours":  [ 
      { 
     "day": "Sun - Thurs:", 
     "time": "9:00AM - 5:00PM" 
    }, 
      { 
     "day": "Fri - Sat:", 
     "time": "9:00AM - 6:00PM" 
    }, 
      { 
     "day": "Slide", 
     "time": "Closed for the season" 
    } 
    ] 
} 

] 

}

注一此文件中的對象應該代表在excel中排成一排。

+0

是否列表對應於記錄中的特定字段?您可以添加一個自定義列,將列表轉換爲記錄並保持記錄原樣。如果您認爲合適,您可以解包記錄。 –

+0

如果我點擊列表,它會顯示一個字符串值,在某些情況下它也是空的。但是對於記錄,它有兩個字段「日」和「時間」(它們都包含字符串值)。該列表不對應於記錄中的任何特定字段。 – kallada

+0

您能否包含您嘗試解析的JSON輸入示例,以及您想要將其轉換爲的輸出表? –

回答

2

我想在這裏分享一個外部鏈接(不確定我是否違反任何政策,如果是這樣,請糾正我),以便其他人也可以使用這些知識。 我的核心問題在www.mrexcel.com/forum/power-bi的幫助下得到了解決。通過這段代碼,我可以創建一個基於excel的工具,通過組合不同的提要爲業務準備數據集。請找爲此所需的代碼(M,而是非常具體的我輸入文件)

let 
    source = Json.Document(File.Contents("d:\path\filename.json")), 
    tabled = Table.FromRecords({source}), 
    expandListField = Table.ExpandListColumn(tabled, "thingstodo"), 
    expandRecField = Table.ExpandRecordColumn(expandListField, "thingstodo", {"propCode", "hours"}, {"propCode", "hours"}), 
    expandList2 = Table.ExpandListColumn(expandRecField, "hours"), 
    fieldForRec = Table.AddColumn(expandList2,"Rec",each if Value.Is([hours], type record) then [hours] else null,type record), 
    fieldForList = Table.AddColumn(fieldForRec, "List",each if Value.Is([hours], type list) then [hours] else null,type list), 
    removed = Table.RemoveColumns(fieldForList, {"hours"}), 
    expandRecField2 = Table.ExpandRecordColumn(removed, "Rec", {"day", "time"}, {"day", "time"}), 
    expandList3 = Table.ExpandListColumn(expandRecField2, "List") 
in 
    expandList3 

請找一個鏈接到下面這個論壇:

http://www.mrexcel.com/forum/power-bi/904936-transforming-json-power-query-mix-list-record-single-column-2.html

感謝

+1

雖然此鏈接可能回答此問題,但最好在此處包含答案的重要部分並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/10396379) –

+0

@Piotr Olaszewski我已經編輯了我的回答,內嵌你的建議。謝謝。 – kallada

相關問題