i need a valid JSON format to request ES. i have a string like
{
time: {
from:now-60d,
mode:quick,
to:now }
}
but when i try to use JSON.parse
i got error because my string should be like
{
time: {
"from":"now-60d",
"mode":"quick",
"to":"now" }
}
so my question, there is any solution to add double quotes around keys and values of my string ?
thanx
Firstly, double quote character is nothing special in regex - it's just another character, so it doesn't need escaping from the perspective of regex. However, because Java uses double quotes to delimit String constants, if you want to create a string in Java with a double quote in it, you must escape them.
to show double quote you can simple use escape character("\") to show it.
We can use the backslash ( \ ) escape character to prevent JavaScript from interpreting a quote as the end of the string. The syntax of \' will always be a single quote, and the syntax of \" will always be a double quote, without any fear of breaking the string.
To place quotation marks in a string in your code In Visual Basic, insert two quotation marks in a row as an embedded quotation mark. In Visual C# and Visual C++, insert the escape sequence \" as an embedded quotation mark. For example, to create the preceding string, use the following code.
See the below example, how it will be done: var ssq = 'It's an example of printing the single quote with string.'; It's an example of printing the single quote with string. In JavaScript, you can use " with a string to display string with double quotation mark. With ", you can use any quote.
Suppose, if we want to add double quotes to a string then we need [ "] escape sequence to escape quotes. Let us suppose if we have a string "Preeti" and simply it will display like this preeti and if we want to add double quotes to string (i.e. display like "preeti" ) then we will write a statement like this ""Preeti"".
// For adding double quotes to OOPS // then we need or add " escape sequence to escape quotes // around both side string. String str2 = " " OOPS " "; System. out. println ("Display String without quotes " + " " + str1); System. out. println ("Display String with quotes " + " " + str2); } }
Double quotes around a string are used to specify a regular expression search (compatible with Perl 5.005, using the Perl-compatible regular expressions library written by Philip Hazel). Regular expressions are a very powerful concept but rather hard to explain from scratch.
maybe you can use :
str.replace(/([a-zA-Z0-9-]+):([a-zA-Z0-9-]+)/g, "\"$1\":\"$2\"");
Here is
Note
In the group [a-zA-Z0-9-]
of characters i use alphabetical
digits
and a -
, maybe you need other so you can use another one
This function will add quotes and remove any extra commas at the end of objects
function normalizeJson(str){return str.replace(/"?([\w_\- ]+)"?\s*?:\s*?"?(.*?)"?\s*?([,}\]])/gsi, (str, index, item, end) => '"'+index.replace(/"/gsi, '').trim()+'":"'+item.replace(/"/gsi, '').trim()+'"'+end).replace(/,\s*?([}\]])/gsi, '$1');}
Edit:
This other function supports json arrays.
It also converts single quotes to double quotes, and it keeps quotes off of numbers and booleans.
function normalizeJson(str){
return str.replace(/[\s\n\r\t]/gs, '').replace(/,([}\]])/gs, '$1')
.replace(/([,{\[]|)(?:("|'|)([\w_\- ]+)\2:|)("|'|)(.*?)\4([,}\]])/gs, (str, start, q1, index, q2, item, end) => {
item = item.replace(/"/gsi, '').trim();
if(index){index = '"'+index.replace(/"/gsi, '').trim()+'"';}
if(!item.match(/^[0-9]+(\.[0-9]+|)$/) && !['true','false'].includes(item)){item = '"'+item+'"';}
if(index){return start+index+':'+item+end;}
return start+item+end;
});
}
I also tested the regex with the safe-regex npm module
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With