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

  • SEARCH
  • Home
  • 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 7065665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:59:01+00:00 2026-05-28T04:59:01+00:00

I want to directly convert a javascript object to string. I used following code.

  • 0

I want to directly convert a javascript object to string. I used following code.

var foo = {};  
foo.test1 = test1;  
foo.test2 = test2;  
foo.test3 = test3;  

var jsonObj = JSON.stringify(foo); 

It works fine but it uses the json2 javascript library. However I need to do this in plain javascript without using any libraries. I know creating the json feed using passed parameters will work like this.

var jsonObj = "{\"test1\":\"" + test1+ "\",\"test2\":\"" + test2+ "\",\"test3\":\"" + test3+ "\"}";

However if the passed parameters(test1, test2 and test3) contains double quotes it will have issues.

What is the best approach to achieve this?

Thank you

  • 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-05-28T04:59:02+00:00Added an answer on May 28, 2026 at 4:59 am

    You should escape the double quotes by performing a String.replace(/"/g, "\\\"") on each key and member. For this to work however, you need to guarantee that you will only have simple strings/ numbers in your JS object.

    FYI, it should be noted that the json2 library will only be used when a native implementation of JSON does not exist; all modern browsers have JSON support build in (IE < 8 is the noticable exception).

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

Sidebar

Related Questions

I want to access a MySQL database directly from JavaScript code in an HTML
I have a Binary tree for a mathematical expression(infix), i want to convert directly
I'm writing the following code to convert SQL to LINQ and then to XML:
i have following code #include <iostream> #include <string> using namespace std; string generate(){ for
If I want to navigate directly to the second state of some flex app,
I have a huge IQueryable that I want to export directly to Excel for
I want to upload files directly to IIS7 (in this case I am using
I want to write some text directly to a file using Flex 3 /
I want to redirect the output of some Windows program directly to gvim basically
I want to create a search box with the search button directly to the

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.