0

I am using tidyjson in R to convert json to data frame. I am not able to go back to previous level/object

"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 }
Jaap
  • 81,064
  • 34
  • 182
  • 193
  • Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Aug 27 '17 at 13:04

1 Answers1

0

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
cole
  • 1,737
  • 2
  • 15
  • 21