string escaping in json object [duplicate] - javascript

This question already has answers here:
How should I escape strings in JSON?
(18 answers)
Closed 3 years ago.
I have this object that I send in response as json.
{"__type":"http:\\/\\/example.com\\/contracts\\/documents\\/rendering\\/instructions\\/1\\/0"}
I want response to be:
{"__type":"http:\/\/example.com\/contracts\/documents\/rendering\/instructions\/1\/0"}
but I get this:
{"__type":"http:\\/\\/example.com\\/contracts\\/documents\\/rendering\\/instructions\\/1\\/0"}
How do I escape string correctly, so I can get response string with only one backslash?

Do this:
let str='{"__type":"http:\\/\\/example.com\\/contracts\\/documents\\/rendering\\/instructions\\/1\\/0"}'
let result = str.replace(/\\/g,'\\');

Related

Extract the array represented as a string [duplicate]

This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Execute JavaScript code stored as a string
(22 answers)
Closed 1 year ago.
i have a string as below and i would like to extract the array between the quotes.
mystring = "['str1','str2']"
I tried it with eval and i do not want to use eval in my code. is there any other neat way to do this ?
function parseString(string) {
return string
.split(",")
.map((str) => str.replace("[", "").replace("]", "").replaceAll("'", ""));
}
This assumes none of array indexes includes character ",".

Disable type validation while JSON.parse [duplicate]

This question already has answers here:
Prevent JSON.parse(data) from cutting off zero digit for String floats
(1 answer)
Stop JSON.parse() from removing trailing zeros from a json string data
(1 answer)
Trailing zeros in javascript
(2 answers)
Closed 2 years ago.
Have String {"a":100.000} and I need to parse it as JSON object.
When I run JSON.parse("{\"a\":100.000}"), output is {a: 100}.
How do I retain the original format of value after parsing it as JSON object?

Regular expression, get data from base64 [duplicate]

This question already has answers here:
How to strip type from Javascript FileReader base64 string?
(5 answers)
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 3 years ago.
I have a base64 data
data:application/pdf;base64,JVBERi0xLj........
I want to get the rest of the data like
JVBERi0xLj........
Following not working, wonder what is the correct approach?
const express = /data:(.*?);base64,(.*?)/g;
const arr = express.exec(base64);
You can simplify and match all the data uri standard :
:([^;]*)[^,]+,(.*)
Your Regex will not match other data urls that are also standard like :
data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh.
data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678

Convert String to nested array of characters [duplicate]

This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Closed 6 years ago.
I have a string passed to me like
var str = "['♫', ['►', 'Play']]";
Multiple entries can be there
Need to convert it to Array.
How can It be done?
PS: No eval() please. and also no Jquery
Note: I have tried JSON.parse, but it didn't work for me. It gives following error:
Unexpected token ' in JSON at position 1
JSON.parse(str.replace(/'/g, '"'))
In other words, fix the quotes so that it is valid JSON, then you will be able to use JSON.parse.

How to get query string parameters from a url [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
How to to get query string parameters from a url
Sample Input -
www.boom.com/?frontend=true&challenge=false
Sample Output -
frontend = true
challenge = false
You could use
String frontEnd=request.getParameter("frontend");
String challenge=request.getParameter("challenge");
to get the values and then use/show them

Categories