遍历 Json List in Excel Power Query 以检索记录
Loop through Json List in Excel Power Query to retrieve record
我有一个强大的查询,它调用 Google 地图 API 并且 returns Json 像这样
let
baseurl = "https://maps.googleapis.com/maps/api/geocode/json?",
cellAddress = Excel.CurrentWorkbook(){[Name="Address"]}[Content]{0}[Column1],
stepOneAdress = Replacer.ReplaceText(cellAddress, "Addrs: ", ""),
noSpaceAdress = Replacer.ReplaceText(stepOneAdress, " ", "%20"),
noCommasAdress = Replacer.ReplaceText(noSpaceAdress, ",", "%2C"),
fullUrl = baseurl&"address="&noCommasAdress&"&key=AIzaSyCwcLo1bl8iTSWhU3vgHNuq3rJHbSGH-Pw",
webdata = Web.Contents(fullUrl),
response = Json.Document(webdata),
results = response[results],
data = results{0}
in
data
数据是这样的
{
"results" : [
{
"address_components" : [
{
"long_name" : "Steamboat Springs",
"short_name" : "Steamboat Springs",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Routt County",
"short_name" : "Routt County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Colorado",
"short_name" : "CO",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "80487",
"short_name" : "80487",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Steamboat Springs, CO 80487, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.5286781,
"lng" : -106.7801651
},
"southwest" : {
"lat" : 40.439399,
"lng" : -106.886848
}
},
"location" : {
"lat" : 40.4849769,
"lng" : -106.8317158
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.5286781,
"lng" : -106.7801651
},
"southwest" : {
"lat" : 40.439399,
"lng" : -106.886848
}
}
},
"place_id" : "ChIJYUZWCYF7QocRfc9uSNGjqBs",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
然后我可以在另一个高级查询中读取该高级查询的“数据”,我试图在其中获取邮政编码数据。
let
data = Coordinates,
address_components = data[address_components],
address_components1 = address_components{6}
in
address_components1
address_component1(我知道可怕的名字,但仍在制作原型)
是一个 Json 记录,然后被另一个查询使用。
但是 Json 列表中的值是硬编码的,如您所见,它是列表中的第六项。但是我发现我想要的邮政编码并不总是在列表的第六位。
记录中有一个类型列表,我想读取它并确定类型是否等于“postal_code”
我不知道如何遍历列表并检查每个项目。
我希望它是这样的
address_component1,
foreach(item in address_components){
type_list = item["types"],
if type_list = "postal_code"
address_component1 = item,
这样循环可以吗?
如果,事实上,您的 json 与您显示的一样,关于:
- 单个邮政编码元素
- 包含一个邮政编码
您可以使用以下代码提取它:
- 首先将
address_components
提取到记录列表中
- 遍历每条记录,看
types
中的第一个元素是不是postal_code
- 如果是,那么return
long_name
如果 json 包含多个 postal_code
,可能需要不同的算法。
let
Source = Json.Document(File.Contents("C:\Users\ron\Desktop\new 3.json")),
//extract the address_components
address_components = Source[results]{0}[address_components],
//find the postal code and extract it
postalCode=List.Accumulate(address_components,"", (state, current)=>
if Record.Field(current,"types"){0} = "postal_code" then state & Record.Field(current,"long_name") else state)
in
postalCode
postalCode
将包含邮政编码作为文本字符串。
我有一个强大的查询,它调用 Google 地图 API 并且 returns Json 像这样
let
baseurl = "https://maps.googleapis.com/maps/api/geocode/json?",
cellAddress = Excel.CurrentWorkbook(){[Name="Address"]}[Content]{0}[Column1],
stepOneAdress = Replacer.ReplaceText(cellAddress, "Addrs: ", ""),
noSpaceAdress = Replacer.ReplaceText(stepOneAdress, " ", "%20"),
noCommasAdress = Replacer.ReplaceText(noSpaceAdress, ",", "%2C"),
fullUrl = baseurl&"address="&noCommasAdress&"&key=AIzaSyCwcLo1bl8iTSWhU3vgHNuq3rJHbSGH-Pw",
webdata = Web.Contents(fullUrl),
response = Json.Document(webdata),
results = response[results],
data = results{0}
in
data
数据是这样的
{
"results" : [
{
"address_components" : [
{
"long_name" : "Steamboat Springs",
"short_name" : "Steamboat Springs",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Routt County",
"short_name" : "Routt County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Colorado",
"short_name" : "CO",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "80487",
"short_name" : "80487",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Steamboat Springs, CO 80487, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.5286781,
"lng" : -106.7801651
},
"southwest" : {
"lat" : 40.439399,
"lng" : -106.886848
}
},
"location" : {
"lat" : 40.4849769,
"lng" : -106.8317158
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.5286781,
"lng" : -106.7801651
},
"southwest" : {
"lat" : 40.439399,
"lng" : -106.886848
}
}
},
"place_id" : "ChIJYUZWCYF7QocRfc9uSNGjqBs",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
然后我可以在另一个高级查询中读取该高级查询的“数据”,我试图在其中获取邮政编码数据。
let
data = Coordinates,
address_components = data[address_components],
address_components1 = address_components{6}
in
address_components1
address_component1(我知道可怕的名字,但仍在制作原型) 是一个 Json 记录,然后被另一个查询使用。
但是 Json 列表中的值是硬编码的,如您所见,它是列表中的第六项。但是我发现我想要的邮政编码并不总是在列表的第六位。
记录中有一个类型列表,我想读取它并确定类型是否等于“postal_code”
我不知道如何遍历列表并检查每个项目。
我希望它是这样的
address_component1,
foreach(item in address_components){
type_list = item["types"],
if type_list = "postal_code"
address_component1 = item,
这样循环可以吗?
如果,事实上,您的 json 与您显示的一样,关于:
- 单个邮政编码元素
- 包含一个邮政编码
您可以使用以下代码提取它:
- 首先将
address_components
提取到记录列表中 - 遍历每条记录,看
types
中的第一个元素是不是postal_code
- 如果是,那么return
long_name
如果 json 包含多个 postal_code
,可能需要不同的算法。
let
Source = Json.Document(File.Contents("C:\Users\ron\Desktop\new 3.json")),
//extract the address_components
address_components = Source[results]{0}[address_components],
//find the postal code and extract it
postalCode=List.Accumulate(address_components,"", (state, current)=>
if Record.Field(current,"types"){0} = "postal_code" then state & Record.Field(current,"long_name") else state)
in
postalCode
postalCode
将包含邮政编码作为文本字符串。