JSON String inside JSON

I want to create a JSON string inside a JSON request. Here is my code

Fiddle

Js

var x = { a: 1, b: 'a sample text', }; var request = { t: JSON.stringify(x), c: 2, r: 'some text' }; console.log(request); 

Can someone help me how to avoid double quotes?

Console

 Object { t: "{"a":1,"b":"a sample text"}", //This creates a problem, double quotes inside double quotes. c: 2, r: "some text" } 

Thanks in advance.

+6
source share
2 answers

This is the way the browser console shows you the value of a string, wrapping double quotes for output. This is completely normal and nothing is broken.

You can test it by converting the JSON string back to an object and using the property.

 console.log( JSON.parse(request.t).b ); // a sample text 
+5
source

No problems. This is just your console.log , which shows all the lines, just dividing with " .

As you say, this request object is used in a JSON request, where it will be JSON.stringify ed at another time, with a valid result

 {"t":"{\"a\":1,\"b\":\"a sample text\"}","c":2,"r":"some text"} 
+3
source

Source: https://habr.com/ru/post/976367/


All Articles