skip to Main Content

I need to pass JSON object to a function in r programming. The following are the various examples of JavaScript objects that can be passed which needed to be converted to JSON in r

Example1 = [
  {name: "Orange", data: {"Tuesday": 3, "Friday": 4}, stack: "fruit"},
  {name: "Apple", data: {"Tuesday": 1, "Friday": 8}, stack: "fruit"},
  {name: "Cucumber", data: {"Tuesday": 3, "Friday": 4}, stack: "vegetable"},
  {name: "Carrot", data: {"Tuesday": 1, "Friday": 8}, stack: "vegetable"}
]

Example2 = [["Washington", "1789-04-29", "1797-03-03"], ["Adams", "1797-03-03", "1801-03-03"]]

I need help converting them into a JSON object in r using jsonlite package.

2

Answers


  1. Chosen as BEST ANSWER

    Thank you @margusl I just resolved Example1

    jsonlite::toJSON('[
      {name: "Orange", data: {"Tuesday": 3, "Friday": 4}, stack: "fruit"},
      {name: "Apple", data: {"Tuesday": 1, "Friday": 8}, stack: "fruit"},
      {name: "Cucumber", data: {"Tuesday": 3, "Friday": 4}, stack: "vegetable"},
      {name: "Carrot", data: {"Tuesday": 1, "Friday": 8}, stack: "vegetable"}
    ]')
    

    But, this is not actually what I want. I wish I could achieve the same output as what I would get in JavaScript when I use JSON.stringify() where the keys are quoted. I hope I could get help here


  2. Javascript code can be evaluated with v8, that code can include JSON.stringify(), but for a list of simple assignment expressions we can set serialize parameter for $eval() method and it will return JSON string:

    library(V8)
    #> Using V8 engine 9.1.269.38
    js <- c(
      ex1 = 'Example1 = [
      {name: "Orange", data: {"Tuesday": 3, "Friday": 4}, stack: "fruit"},
      {name: "Apple", data: {"Tuesday": 1, "Friday": 8}, stack: "fruit"},
      {name: "Cucumber", data: {"Tuesday": 3, "Friday": 4}, stack: "vegetable"},
      {name: "Carrot", data: {"Tuesday": 1, "Friday": 8}, stack: "vegetable"}
    ]',
      ex2 = 'Example2 = [["Washington", "1789-04-29", "1797-03-03"], ["Adams", "1797-03-03", "1801-03-03"]]'
    )
    
    ctx <- v8()
    # evaluate and serialize js return object
    json_1 <- ctx$eval(js[1], serialize = TRUE) 
    jsonlite::validate(json_1)
    #> [1] TRUE
    json_1
    #> [1] "[{"name":"Orange","data":{"Tuesday":3,"Friday":4},"stack":"fruit"},{"name":"Apple","data":{"Tuesday":1,"Friday":8},"stack":"fruit"},{"name":"Cucumber","data":{"Tuesday":3,"Friday":4},"stack":"vegetable"},{"name":"Carrot","data":{"Tuesday":1,"Friday":8},"stack":"vegetable"}]"
    
    # or apply ctx$eval() on the the list of expressions
    json_lst <- lapply(js, ctx$eval, serialize = TRUE)
    str(json_lst)
    #> List of 2
    #>  $ ex1: chr "[{"name":"Orange","data":{"Tuesday":3,"Friday":4},"stack":"fruit"},{"name":"Apple","data":{"| __truncated__
    #>  $ ex2: chr "[["Washington","1789-04-29","1797-03-03"],["Adams","1797-03-03","1801-03-03"]]"
    

    Created on 2023-07-23 with reprex v2.0.2

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search