Agree with @Jaap that your question could use some work to make it more helpful for other users and to those helping you. In any case, this is an oft-recurring question with tidyjson
. Have a look at the updated documentation here that we are hoping to submit to CRAN soon. In particular, the discussion on this issue might be helpful. This is a problem that definitely needs to be improved (as well as the verbosity of spread_values
/ etc. (spread_all
is an alternative in the new version, but it is broken by dplyr
> 0.7 at present).
If you want to use the dev version of tidyjson
, try devtools::install_github('jeremystan/tidyjson')
. In any case, I think the problem you provide has a workable solution. Your JSON was invalid, so I took some liberties in cleaning it up. Websites like JSON Pretty Print can be helpful for ensuring valid JSON structure.
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