简体   繁体   中英

Make json easily readable (R)

Suppose I have a variable ouput which contains a json of the form:

{"hello1":["bla1"],"hello2":["bla2"],"hello3":{"hello31":{"hello311":[7078],"hello312":[3429]},"hello32":{"hello321":[10],"hello322":[6]},"hello33":{"hello331":[4.6317],"hello332":[2.6322]}}}

What can I do to transform ouput such as it can be easily readable? I would like something like this:

hello1 : bla1
hello2 : bla2
hello3 :
         hello31 : hello311 : 7078
                   hello312 : 3429
         hello32 : hello321 : 10
                   hello322 : 6
         hello33 : hello331 : 4.6317
                   hello332 : 2.6322

If you just want to inspect the json , you could use the package listviewer , which renders a nice widget in RStudio.

# install.packages("listviewer")

output <- '{"hello1":["bla1"],"hello2":["bla2"],"hello3":{"hello31":{"hello311":[7078],"hello312":[3429]},"hello32":{"hello321":[10],"hello322":[6]},"hello33":{"hello331":[4.6317],"hello332":[2.6322]}}}'

listviewer::jsonedit(output)

You can get something similar to your output using the prettify function in jsonlite .

# Your object
my_json_object <- '{"hello1":["bla1"],"hello2":["bla2"],"hello3":{"hello31":{"hello311":[7078],"hello312":[3429]},"hello32":{"hello321":[10],"hello322":[6]},"hello33":{"hello331":[4.6317],"hello332":[2.6322]}}}'


prettify(my_json_object, indent = 2)
{
"hello1": [
    "bla1"
],
"hello2": [
    "bla2"
],
"hello3": {
    "hello31": {
        "hello311": [
            7078
        ],
        "hello312": [
            3429
        ]
    },
    "hello32": {
        "hello321": [
            10
        ],
        "hello322": [
            6
        ]
    },
    "hello33": {
        "hello331": [
            4.6317
        ],
        "hello332": [
            2.6322
        ]
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM