0

I have javascript variable var result as following which has java variable that have JSON data like this

var result = <%=JsonData1%> ;
alert(result.toSource());

Above code similar to this code as showing on alert message

 var result= [{"year":"12","value":"6694"},{"year":"13","value":"50"},{"year":"08","value":"4776"},{"year":"09","value":"29006"},{"year":"10","value":"1751"}];

but I need to place a single quote on JSON data

'[{"year":"12","value":"6694"},{"year":"13","value":"50"},{"year":"08","value":"4776"},{"year":"09","value":"29006"},{"year":"10","value":"1751"}]'

and place it in new javascript variable like this

var json_pre =  result;
alert(json_pre);

but when I change my result data that placed in new json_pre variable as in this link How to add single quote in the variable in Javascript?

var json_pre =  "'" +result+ "'";
alert(json_pre.toSource());

then json data shows me like this on the alert message

'[{year:(new Date(-2208058200000)), value:6694}, {year:(new Date(-2207971800000)), value:50}, {year:(new Date(-2208403800000)), value:4776}, {year:(new Date(-2208317400000)), value:29006}, {year:(new Date(-2208231000000)), value:1751}]'

As I have tried every method that given me on that link.So anyone can help me in this?

10
  • 2
    My question would be why do you need to wrap that in single quotes? Commented Mar 15, 2018 at 9:39
  • I need that type of JSON further in code. Commented Mar 15, 2018 at 9:48
  • 1
    That's the thing, I don't think you do. JSON.stringify(result) is what you need, and it returns a JSON string. No need to wrap it in quotes, since it already is a string. Commented Mar 15, 2018 at 9:49
  • Adding server-side code is useless, we need to see the result, how it ends up in your browser's source view (press Ctrl+U). Anyway, did you try alert(JSON.stringify(result)); right after setting var result = <%=JsonData1%>;? Edit: I did. Commented Mar 15, 2018 at 9:56
  • yeah it showing me same required data but it changes when I placed it in a new variable with single quotes and I need that type of json in the further code for the different purpose Commented Mar 15, 2018 at 9:57

2 Answers 2

0

result is not JSON, but a JavaScript object.

What you need to do is to stringify your object to JSON and then add the quotes:

var result = [{"year":"12","value":"6694"},{"year":"13","value":"50"},{"year":"08","value":"4776"},{"year":"09","value":"29006"},{"year":"10","value":"1751"}];
var result_pre = "'" + JSON.stringify(result) + "'";
console.log(result_pre);

But it is questionable why you would even need that.

Sign up to request clarification or add additional context in comments.

8 Comments

i am getting like this on alert message
'[{"year":"1900-01-11T18:30:00.000Z","value":6694},{"year":"1900-01-12T18:30:00.000Z","value":50},{"year":"1900-01-07T18:30:00.000Z","value":4776},{"year":"1900-01-08T18:30:00.000Z","value":29006},{"year":"1900-01-09T18:30:00.000Z","value":1751}]'
@kunal That is not possible with the code I provided. There must be something else in your code that does it, but you did not show your code.
@kunal As I already said "That is not possible with the code I provided". Your code or template engine obviously changes the string and in addition there is no (standard) toSource function on strings.
okay I just want to know one thing, is that code var result_pre = "'" + JSON.stringify(result) + "'"; changes json into string format variable in javascript
|
0

You need to understand the single quotes does not make an object a string. When you are trying to store it in a variable I guess you are making the same mistake again.

when you do this

a={"key":"value"}
b="'" + a + "'";

You get a string representation for consoles, if you want a JSON string to be stored in a variable you need to do this

var json = JSON.stringify(result);
alert(json);

You don't need quotes for that.

2 Comments

This is a duplicate of str's answer.
Nobody downvoted you for that answer. Comments are here also to guide other users to the best answer, don't take them personnally. You can indeed delete it as it brings nothing new, or just let it here as it won't harm anybody

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.