Search code examples
javascriptjsonweka

How do you write json in .arff format


I have the following json file.

[
    {
        "y": 1.544937286376953, 
        "x": 0.0736468505859375, 
        "z": 10.19739440917969, 
        "timestamp": 1413232199331.14
    }, 
    {
        "y": 2.492466888427734, 
        "x": 0.7253915405273438, 
        "z": 11.33457962036133, 
        "timestamp": 1413232199831.21
    }
]

and both the objects in this list translate into High value. similar to the weather example below, for example both of these objects which are:

{
    "y": 1.544937286376953, 
    "x": 0.0736468505859375, 
    "z": 10.19739440917969, 
    "timestamp": 1413232199331.14
}

and

{
    "y": 2.492466888427734, 
    "x": 0.7253915405273438, 
    "z": 11.33457962036133, 
    "timestamp": 1413232199831.21
}

if they appear at the same time, then set the other property lets say for example Velocity to High.

How can I write it into something like the weather example below:

@relation weather

@attribute outlook {sunny, overcast, rainy}
@attribute temperature numeric
@attribute humidity numeric
@attribute windy {TRUE, FALSE}
@attribute play {yes, no}

@data
sunny,85,85,FALSE,no
sunny,80,90,TRUE,no

where is my attribute is a list of objects.

my attribute is something like @attribute accelerator [{numeric,numeric,numeric},{numeric, numeric,numeric}]

Does anyone know what should I do? Does my question actually makes any sense at all?


Solution

  • It seems to me that you want to do two things:

    1. Convert JSON Data to .arff
    2. Write composite attributes to the arff file

    I don't know if arff files support #2.

    Here is some code to transform your JSON into arff (#1 ) In R:

    library(RWeka)
    library(rjson)
    
    
    json = rjson::fromJSON('[{
             "y": 1.544937286376953, 
             "x": 0.0736468505859375, 
             "z": 10.19739440917969, 
             "timestamp": 1413232199331.14
         }, 
         {
             "y": 2.492466888427734, 
             "x": 0.7253915405273438, 
             "z": 11.33457962036133, 
             "timestamp": 1413232199831.21
         }]')
    
    str(json) # show internal representation
    
    # replace nulls, optional
    json <- lapply(json, function(x) {
            x[sapply(x, is.null)] <- NA
            unlist(x)
    })
    
    # convert to data frame
    mydf <- data.frame(do.call("rbind", json))
    
    # add some more attributes. I've just made up this business logic
    mydf["accelerator"] = sqrt(mydf$x^2 + mydf$y^2 + mydf$z^2)
    # here the new "accelerator" attribute is high if it is higher than 11    
    mydf["accelerator_high"] = ifelse(mydf["accelerator"]<=11,"No","Yes")
    
    RWeka::write.arff(mydf, "myfile.arff")
    

    Resulting arff file:

    @relation R_data_frame
    
    @attribute y numeric
    @attribute x numeric
    @attribute z numeric
    @attribute timestamp numeric
    @attribute accelerator numeric
    @attribute accelerator_high string
    
    @data
    1.544937,0.073647,10.197394,1413232199331.13984,10.314025,No
    2.492467,0.725392,11.33458,1413232199831.209984,11.628038,Yes