Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8201611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:52:24+00:00 2026-06-07T06:52:24+00:00

I am writing some helper functions to convert my R variables to JSON. I’ve

  • 0

I am writing some helper functions to convert my R variables to JSON. I’ve come across this problem: I would like my values to be represented as JSON arrays, this can be done using the AsIs class according to the RJSONIO documentation.

x = "HELLO"
toJSON(list(x = I(x)), collapse="")

"{ \"x\": [ \"HELLO\" ] }"

But say we have a list

y = list(a = "HELLO", b = "WORLD")
toJSON(list(y = I(y)), collapse="")

"{ \"y\": {\n \"a\": \"HELLO\",\n\"b\": \"WORLD\" \n} }"

The value found in y -> a is NOT represented as an array. Ideally I would have

"{ \"y\": [{\n \"a\": \"HELLO\",\n\"b\": \"WORLD\" \n}] }"

Note the square brackets. Also I would like to get rid of all “\n”s, but collapse does not eliminate the line breaks in nested JSON. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:52:26+00:00Added an answer on June 7, 2026 at 6:52 am

    try writing as

    y = list(list(a = "HELLO", b = "WORLD"))
    test<-toJSON(list(y = I(y)), collapse="")
    

    when you write to file it appears as:

    { "y": [
     {
     "a": "HELLO",
    "b": "WORLD" 
    } 
    ] }
    

    I guess you could remove the \n as

    test<-gsub("\n","",test)
    

    or use RJSON package

    > rjson::toJSON(list(y = I(y)))
    [1] "{\"y\":[{\"a\":\"HELLO\",\"b\":\"WORLD\"}]}"
    

    The reason

    > names(list(a = "HELLO", b = "WORLD"))
    [1] "a" "b"
    > names(list(list(a = "HELLO", b = "WORLD")))
    NULL
    

    examining the rjson::toJSON you will find this snippet of code

      if (!is.null(names(x))) 
                return(toJSON(as.list(x)))
            str = "["
    

    so it would appear to need an unnamed list to treat it as a JSON array. Maybe RJSONIO is similar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing some helper functions in Android 3.0's renderscript, and have come
Im writing some helper functions for a project im working on. I've always wanted
I'm writing some helper functions that take a SqlDataReader and process the results. Now
I have some PowerShell helper functions in a file. I'd like to make them
I'm contemplating writing some helper functions to make it easier to do simple changes
I'm writing some decision-making AI for a game, and I've come up with the
I'm currently writing some general purpose .net libraries that contain usual helper classes. For
I have a browser helper object on IE that have some clipboard history functions.
I'm in the process of writing a simple prototype in Node.js with some helper
I wrote these helper functions a few weeks ago and I feel like I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.