Writing JSON children in R

I have a data set that I would like to group in JSON.

                       address         city.x state.x latitude.x longitude.x
    1 5601 W. Slauson Ave. #200    Culver City      CA   33.99718  -118.40145
    2                 PO 163005         Austin      TX   30.31622   -97.85877
    3 10215 W. Jamesburg Street        Wichita      KS   37.70063   -97.43430
    4         14556 Newport Ave         Tustin      CA   33.74165  -117.82127
    5      2496 Falcon Crescent Virginia Beach      VA   36.83840   -76.02862
    6   1306 Wilshire Boulevard   Santa Monica      CA   34.03216  -118.49022

I would like to group together address and lat/long and put it all under the category of company.

I would like it to look like this:

     {company: {address: {address:  "5601 W. Slauson Ave. #200" ,
                          city.x:  "Culver City" ,
                          state.x:  "CA"}},
               {geo: {latitude: "33.99718",
                      longitude: "-118.40145"}}},

     {company: {address: {address:  "PO 163005" ,
                          city.x:  "Austin" ,
                          state.x:  "TX"}},
               {geo: {latitude: "30.31622",
                      longitude: "-97.85877"}}},



    structure(list(address = c("5601 W. Slauson Ave. #200", "PO 163005", 
    "10215 W. Jamesburg Street", "14556 Newport Ave", "2496 Falcon Crescent", 
    "1306 Wilshire Boulevard"), city.x = c("Culver City", "Austin", 
    "Wichita", "Tustin", "Virginia Beach", "Santa Monica"), state.x = c("CA", 
    "TX", "KS", "CA", "VA", "CA"), latitude.x = c(33.997179, 30.316223, 
    37.700632, 33.741651, 36.838398, 34.032159), longitude.x = c(-118.40145, 
    -97.85877, -97.4343, -117.82127, -76.02862, -118.49022)), .Names = c("address", 
    "city.x", "state.x", "latitude.x", "longitude.x"), class = "data.frame", row.names = c(NA, 
    6L))

Any help would be appreciated!

Answers


The following code should output what you want:

for (i in 1:nrow(df)){
  cat ("{company:{address:{adress:\t\"",df$address[i],
       "\",\n\t\tcity.x:\t\"", df$city.x[i],
       "\",\n\t\tstate.x:\t \"", df$state.x[i],
       "\"}}\n\t {geo:{\tlatitude: \"", df$latitude[i],
       "\",\n\t\tlongitude: \"", df$longitude[i],
       "\"}}},\n", sep="")
}

with df as your data frame.


Another option is to use the rjson package.

require(rjson)
# This is necessary to avoid duplication of labels in the JSON output
names(df) <- NULL

reshaped <- apply(df, 1, FUN=function(x){list(address=list(
                                                  address = x[1],
                                                  city = x[2],
                                                  state = x[3]),
                                            coords=list(
                                                  latitude = x[4],
                                                  longitude = x[5]))})
result <- toJSON(reshaped)

The only difference from what you requested is that instead of having "company" as the root it will have sequential numbers. You could change it by changing the row names of your data (using rownames), but R does not support duplicate row names... the closest that I got was using

rownames(df) <- paste("company", 1:nrow(df), collapse="")

and maybe with a little regexp magic you could strip the numbers in the output string...


Need Your Help

bash: display only files that contain a date in their file name

regex bash

I am trying to figure out the best method to accomplishing this task, where I have a directory that has multiple files with varying file name formats, and I need to parse out those files that have ...

How to show link or action column in Kendo Grid

kendo-ui kendo-grid

I am using Kendo Grid and I want link when click columns. I want field: "Name"

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.