2
votes

I have plenty of json objects under the same format in one json file like below. And I want to convert them into R dataframe and then to extract all the value of lantency.But when I enter the command

json_data <- fromJSON(file=json_flie)

only the first json object is stored in the dataframe, so what should I do??? Thanks!

{"task":[{"type":"ping","id":1,"value":" 159 159 152 153 149 147 150 151 148 149","IsFinished":true},{"type":"latency","id":2,"value":147,"IsFinished":true},{"type":"throughput","id":3,"value":"","IsFinished":false},{"type":"DNS","id":4,"value":12,"IsFinished":true}],"measurementTimes":10,"url":""}{"task":[{"type":"ping","id":1,"value":" 166 165 179 181 159 162 166 159 161 162","IsFinished":true},{"type":"latency","id":2,"value":159,"IsFinished":true},{"type":"throughput","id":3,"value":"","IsFinished":false},{"type":"DNS","id":4,"value":7,"IsFinished":true}],"measurementTimes":10,"url":""}{"task":[{"type":"ping","id":1,"value":" 172 172 159 160 159 159 159 158 160 162","IsFinished":true},{"type":"latency","id":2,"value":158,"IsFinished":true},{"type":"throughput","id":3,"value":"","IsFinished":false},{"type":"DNS","id":4,"value":14,"IsFinished":true}],"measurementTimes":10,"url":""}{"task":[{"type":"ping","id":1,"value":" 182 192 171 184 160 159 156 157 180 171","IsFinished":true},{"type":"latency","id":2,"value":156,"IsFinished":true},{"type":"throughput","id":3,"value":"","IsFinished":false},{"type":"DNS","id":4,"value":26,"IsFinished":true}],"measurementTimes":10,"url":""}{"task":[{"type":"ping","id":1,"value":" 158 186 168 189 190 233 168 160 188 157","IsFinished":true},{"type":"latency","id":2,"value":157,"IsFinished":true},{"type":"throughput","id":3,"value":"","IsFinished":false},{"type":"DNS","id":4,"value":1,"IsFinished":true}],"measurementTimes":10,"url":""}

1
Can you add linebreaks before the {"task parts and then lapply as per here: stackoverflow.com/questions/27023972/…thelatemail
@MinzhaoLyu: Do the JSON objects in the file have a delimiter to separate them (e.g. a newline)?Alex Woolford
@AlexWoolford - looking at the editable source of the question, my guess is no. It's one big long run of characters without newlines.thelatemail
Your JSON appears to be malformed, and it has more than one root-level element. Where did you get it?Tim Biegeleisen

1 Answers

3
votes

Your input JSON is malformed, and has multiple elements "task" at the root level. This is akin to defining an XML document with more than one root, which is of course not allowed. If you create an outer element which contains an array of "task" elements, then you will be able to successfully load the file into R using fromJSON. Here is what the file should look like:

{
    "root" : [
        {
            "task":
            [
                {"type":"ping","id":1,"value":" 159 159 152 153 149 147 150 151 148 149","IsFinished":true},
                {"type":"latency","id":2,"value":147,"IsFinished":true},
                {"type":"throughput","id":3,"value":"","IsFinished":false},
                {"type":"DNS","id":4,"value":12,"IsFinished":true}
            ],
            "measurementTimes":10,
            "url":""
        },
        {
            "task":
            [
                {"type":"ping","id":1,"value":" 166 165 179 181 159 162 166 159 161 162","IsFinished":true},
                {"type":"latency","id":2,"value":159,"IsFinished":true},
                {"type":"throughput","id":3,"value":"","IsFinished":false},\
                {"type":"DNS","id":4,"value":7,"IsFinished":true}
            ],
            "measurementTimes":10,
            "url":""
        },

    ... and so on for other entries

    ]
}

Here is what I saw in the R console:

> summary(json_data)
      Length Class  Mode
root 5      -none- list

And entering the variable name json_data gave me a dump on the entire JSON structure.