How do I remove double quote "" from JSON Value [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am doing my Javascript. And just imported JSON and printed it out with JSON.stringfy
I am using that site because the JSON contains the URL which I want to link.
I am not getting what I really need as the output.
description": "www.site.com"
This is what I get in my console:
"www.site.com"
This is what I actually want, and remove the quotes.
www.site.com
Please help!

Am not sure what your requirement is but you can do this.
Try Replace function;
const obj = {description: "www.site.com"};
JSON.stringify(obj.description).replace(/"/g,"");

var someStr = 'He said "Hello, my name is Foo"';
console.log(someStr.replace(/['"]+/g, ''));
That should do the trick... (if your goal is to replace all double quotes).

Related

How to parse object array that has been stringified using backslashes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I have been searching for the correct regex for converting this string to JSON. It works if there is a single object in the array, but I believe the comma is messing it up somehow.
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
const obj = JSON.parse(json.replace(/("[^"]*"\s*:\s*)(\d{17,})/g, '$1"$2"'));
console.log(obj);
There was nothing wrong except that the list of objects was not contained within square brackets
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
console.log(JSON.parse(`[${json}]`))

Convert String Object to real Object (without quotes) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I'm having some trouble converting an object of type string to a real object.
Here is my example (it's a string):
{ id: true,translations:{id: true,text: true,language:{id: true,languageCode: true}},createdAt: true }
When I JSON.Stringify it I get:
"{ id: true,translations:{id: true,text: true,language:{id: true,languageCode: true}},createdAt: true }"
And when I JSON.Parse it I get the same result as the example above, I also did console log the type of it and I get it's of type string
Any Ideas on how I should convert it to a real object?
Thanks in advance
By far the easiest way to handle this is using the JSON5 library
const string="{ id: true,translations:{id: true,text: true,language:{id: true,languageCode: true}},createdAt: true }";
const object = JSON5.parse(string);
console.log(object)
<script src="https://unpkg.com/json5#2.2.1/dist/index.js"></script>

Remove First and Last double Quotes from "["Morning Shift","Day Shift"]" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to remove First and Last Quotes from this array Using JavaScript:
"["Morning Shift","Day Shift"]"
After Remove quotes output look like this
["Morning Shift","Day Shift"]
You can try using JSON.parse():
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.
Demo:
var data = `["Morning Shift","Day Shift"]`;
data = JSON.parse(data);
console.log(data);

Why does JSON.parse() add numbers in front of elements? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a string where I build my build my "json-like" format, like
_toBeFormated =
[
{"foor":"bar","foo":"bar","foo":["bar,bar"]},
{"foor":"bar","foo":"bar","foo":["bar,bar"]},
{"foor":"bar","foo":"bar","foo":["bar,bar"]}
]
But after calling JSON.parse like _afterFormat = JSON.parse(_toBeFormated), my structure looks like the following:
_afterFormat =
0:{"foor":"bar","foo":"bar","foo":["bar,bar"]},
1:{"foor":"bar","foo":"bar","foo":["bar,bar"]},
2:{"foor":"bar","foo":"bar","foo":["bar,bar"]}
If I try to change to JSON Format at the beginning, like leaving out [ ], if failes to parse, also it looks like valid JSON to me. What am I missing, or why does it add the numbers at the beginning?
It doesn't add numbers. The data structure is an array. The tool you are using to look at the array is showing the index of each entry.

How to replace new line break with '\n' [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm getting
Uncaught SyntaxError: Unexpected token ILLEGAL
And I checked that because of this
currentMd: '<div>#hehe
</div>'
The two extra new line that is in the currentMd breaking it, so how can I instead replacing that new lines with \n ??
So it will be like this instead
currentMd: '<div>#hehe\n\n</div>'
In javascript or jQuery would be fine.
It may be not clear that the content is actually just an example here.
The actual content is being retrieved from a database, so in the database there I won't have control and the user will always press enter anyway.
EDIT:
I might have not been very clear on my question here.
The thing is that this content that being assign to contentMd it is being retrieved from nodejs which I have no control on what is going in there.
What about:
var your_content = "<div>#hehe\
\
\
</div>";
var replaced_text = your_content.replace(/\n|\s/g, "");
DEMO: http://jsfiddle.net/A4Hk8/
Go into your text editor and replace the line breaks by hand. Unless this code is computer-generated, or you somehow managed to make this bug in thousands of lines of code before catching it, that should be good enough.
That's exactly how you do it:
> '<div>#hehe\n\n</div>'
"<div>#hehe
</div>"

Categories

Resources