我在R中使用tidyjson将json转换为数据帧。我无法返回上一级/对象
"ShipmentID" : 0031632569, "ShipmentType" : Cross-border, "ShipmentStatus" : Final, "PartyInfo" : -[ -{ "Type" : Consignee, "Code" : 0590000001, "Name" : HP Inc. C/O XPOLogistics, "Address" : -{ "AddressLine" : -[ 4000 Technology Court ] }, "City" : -{ "CityName" : Sandston, "CityCode" : -[ -{ "value" : USSAX, "Qualifier" : UN } ], "State" : VA, "CountryCode" : US, "CountryName" : United States }
答案 0 :(得分:0)
同意@Jaap,您的问题可以使用一些工作,使其对其他用户和帮助您的人更有帮助。无论如何,tidyjson
这是一个经常出现的问题。请查看我们希望尽快提交给CRAN的更新文档here。特别是,关于this issue的讨论可能会有所帮助。这是一个肯定需要改进的问题(以及spread_values
/等的详细程度。spread_all
是新版本的替代品,但它被dplyr
&gt打破了; 0.7目前)。
如果您想使用tidyjson
的开发版,请尝试使用devtools::install_github('jeremystan/tidyjson')
。无论如何,我认为您提供的问题有一个可行的解决方案。你的JSON无效,所以我采取了一些自由来清理它。像JSON Pretty Print这样的网站可以帮助确保有效的JSON结构。
library(tidyjson)
library(dplyr)
raw_json <- "{\"ShipmentID\" : \"0031632569\", \"ShipmentType\" : \"Cross-border\", \"ShipmentStatus\" : \"Final\", \"PartyInfo\" : [{ \"Type\" : \"Consignee\", \"Code\" : \"0590000001\", \"Name\" : \"HP Inc. C/O XPOLogistics\", \"Address\": { \"AddressLine\" : [ \"4000 Technology Court\" ] }, \"City\" : { \"CityName\" : \"Sandston\", \"CityCode\" : [ { \"value\" : \"USSAX\", \"Qualifier\" : \"UN\" } ], \"State\" : \"VA\", \"CountryCode\" : \"US\", \"CountryName\" : \"United States\" }}]}"
prep <- raw_json %>%
spread_values(
ShipmentID = jstring(ShipmentID)
, ShipmentType = jstring(ShipmentType)
, ShipmentStatus = jstring(ShipmentStatus)) %>%
enter_object("PartyInfo") %>%
gather_array("PartyInfoID") %>%
spread_values(
Type = jstring(Type)
, Code = jstring(Code)
, Name = jstring(Name)
, CityName = jstring(City, CityName)
, CityState = jstring(City, State)
, CityCountryCode = jstring(City, CountryCode)
, CityCountryName = jstring(City, CountryName)
)
addressline <- prep %>%
enter_object(Address, AddressLine) %>%
gather_array("AddressLineID") %>%
append_values_string("AddressLine") %>%
select(document.id, AddressLineID, AddressLine)
citycode <- prep %>%
enter_object(City, CityCode) %>%
gather_array("CityCodeID") %>%
spread_values(
CityCode = jstring(value)
, CityCodeQualifier = jstring(Qualifier)) %>%
select(document.id, CityCodeID, CityCode, CityCodeQualifier)
prep %>%
left_join(addressline, by = "document.id") %>%
left_join(citycode, by = "document.id")
#> document.id ShipmentID ShipmentType ShipmentStatus PartyInfoID Type
#> 1 1 0031632569 Cross-border Final 1 Consignee
#> Code Name CityName CityState CityCountryCode
#> 1 0590000001 HP Inc. C/O XPOLogistics Sandston VA US
#> CityCountryName AddressLineID AddressLine CityCodeID CityCode
#> 1 United States 1 4000 Technology Court 1 USSAX
#> CityCodeQualifier
#> 1 UN