0

I am using a third party response in json and I want to analyse it using r. To do so I need to transform the json into an r data frame. For instance my json might look like this:

{
    "useless_info1": "trash1",
    "useless_info2": "trash2",
    "useless_info3": "trash3",
    "usefull_info": [
        {
            "color": "red",
            "sizes": [
                "height": 128,
                "width": 40
            ],
            "flavour": "bitter"
        },
        {
            "color": "blue",
            "sizes": [
                "height": 30,
                "width": 10
            ],
            "flavour": "sweet"
        },
    ]
}

I am looking for something like this:

color     sizes_height     sizes_width    flavor
--------  ---------------  -------------  ---------
red       128              40             bitter
blue      30               10             sweet
1
  • 1
    Your JSON is invalid. Is that intentional, or a mistake? Commented Nov 28, 2016 at 14:52

1 Answer 1

3

Consider this:

library(jsonlite)

json = '{
    "useless_info1": "trash1",
    "useless_info2": "trash2",
    "useless_info3": "trash3",
    "usefull_info": [
        {
            "color": "red",
            "sizes": {
                "height": 128,
                "width": 40
            },
            "flavour": "bitter"
        },
        {
            "color": "blue",
            "sizes": {
                "height": 30,
                "width": 10
            },
            "flavour": "sweet"
        }
    ]
}'

fromJSON(json)$usefull_info
#>   color sizes.height sizes.width flavour
#> 1   red          128          40  bitter
#> 2  blue           30          10   sweet
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.