我正在寻找一个json对象的嵌套。有两种方法可以做到这一点(当然可以简化事情)–一种方法是压缩数据,另一种方法是重复数据。使用一个非常基本的示例:
d = {
"ID": 12345,
"Genres": {
"Genre": [
{
"[@FacebookID]": "6003161475030",
"Value": "Comedy"
},
{
"[@FacebookID]": "6003172932634",
"Value": "TV-Show"
}
]
}
第一种冷凝方法将执行以下操作:
d = {
"Genres.Genre[@FacebookID]": ["6003161475030", "6003172932634"]
"Genres.Genre.Value": ["Comedy", "TV-Show"]
}
重复的第二种方法可以做到:
d = [{
"ID": 12345,
"Genres": {
"Genre": {
"[@FacebookID]": "6003161475030",
"Value": "Comedy"
}
}
},[{
"ID": 12345,
"Genres": {
"Genre": {
"[@FacebookID]": "6003172932634",
"Value": "TV-Show"
}
}
}]
给出以下json数据结构:
{
"Item": {
"ID": "288917",
"Main": {
"Platform": "iTunes",
"PlatformID": "353736518",
"Type": "TVEpisode",
"TVSeriesID": "262603760"
},
"Info": {
"BaseURL": "https://itunes.apple.com/us/tv-season/id353187108?i=353736518",
"EpisodeNumber": "5"
},
"Genres": {
"Genre": [
{
"[@FacebookID]": "6003161475030",
"Value": "Comedy"
},
{
"[@FacebookID]": "6003172932634",
"Value": "TV-Show"
}
]
},
"Products": {
"Product": [
{
"[@Country]": "CA",
"URL": "https://itunes.apple.com/ca/tv-season/id353187108?i=353736518",
"Offers": {
"Offer": [
{
"[@Type]": "HDBUY",
"Price": {
"[@InUSD]": "2.56",
"Value": "3.49"
},
"Currency": "CAD"
},
{
"[@Type]": "SDBUY",
"Price": {
"[@InUSD]": "1.83",
"Value": "2.49"
},
"Currency": "CAD"
}
]
}
},
{
"[@Country]": "FR",
"URL": "https://itunes.apple.com/fr/tv-season/id353187108?i=353736518",
"Rating": "Tout public",
"Offers": {
"Offer": [
{
"[@Type]": "HDBUY",
"Price": {
"[@InUSD]": "2.85",
"Value": "2.49"
},
"Currency": "EUR"
},
{
"[@Type]": "SDBUY",
"Price": {
"[@InUSD]": "2.28",
"Value": "1.99"
},
"Currency": "EUR"
}
]
}
}
]
}
}
}
我希望能够调用一个根据设置压缩或展平字段的函数。这将是输出示例:
# default unnest method is condense
unnest(data, repeated_fields=['Genre', 'Products.Product[@Country]', 'Products.Product.Offers.Offer[@Type]')
结果将是(并解释了第一项):
# contains four objects instead of one, because we have repeated on
# two fields, each of those having two values.
[{
"ID": 288917,
"Main.Platform": "iTunes",
"Main.PlatformID": "353736518",
# use default unnest method of condense if not specified
"Genres.Genre": ["Comedy", "TV-Show"],
"Genres.Genre[@FacebookID]": ["6003161475030", "6003161475030"],
# unnest by repeating as it's specified
"Product.Product[@Country]": "CA"
"Products.Product.URL": "https://itunes.apple.com/ca/tv-season/id353187108?i=353736518",
# again, we unnest by repeating since this has been specified
"Products.Product.Offers.Offer[@Type]": "HDBUY"
"Products.Product.Offers.Offer.Price": "3.49"
"Products.Product.Offers.Offer.Currency": "CAD"
},
{
"ID": 288917,
"Main.Platform": "iTunes",
"Main.PlatformID": "353736518",
"Genres.Genre": ["Comedy", "TV-Show"],
"Genres.Genre[@FacebookID]": ["6003161475030", "6003161475030"],
"Product.Product[@Country]": "CA"
"Products.Product.URL": "https://itunes.apple.com/ca/tv-season/id353187108?i=353736518",
"Products.Product.Offers.Offer[@Type]": "SDBUY"
"Products.Product.Offers.Offer.Price": "2.49"
"Products.Product.Offers.Offer.Currency": "CAD"
},
{
"ID": 288917,
"Main.Platform": "iTunes",
"Main.PlatformID": "353736518",
"Genres.Genre": ["Comedy", "TV-Show"],
"Genres.Genre[@FacebookID]": ["6003161475030", "6003161475030"],
"Product.Product[@Country]": "FR"
"Products.Product.URL": "https://itunes.apple.com/fr/tv-season/id353187108?i=353736518",
"Products.Product.Offers.Offer[@Type]": "HDBUY"
"Products.Product.Offers.Offer.Price": "2.49"
"Products.Product.Offers.Offer.Currency": "EUR"
},
{
"ID": 288917,
"Main.Platform": "iTunes",
"Main.PlatformID": "353736518",
"Genres.Genre": ["Comedy", "TV-Show"],
"Genres.Genre[@FacebookID]": ["6003161475030", "6003161475030"],
"Product.Product[@Country]": "FR"
"Products.Product.URL": "https://itunes.apple.com/fr/tv-season/id353187108?i=353736518",
"Products.Product.Offers.Offer[@Type]": "SDBUY"
"Products.Product.Offers.Offer.Price": "1.99"
"Products.Product.Offers.Offer.Currency": "EUR"
}]
我的方法是对每个重复的字段使用一堆deepcopy
格。这是我到目前为止的示例,它凝聚了所有非重复的字段。