regular expression add double quotes around values and keys in javascript

11,792

Solution 1

maybe you can use :

str.replace(/([a-zA-Z0-9-]+):([a-zA-Z0-9-]+)/g, "\"$1\":\"$2\"");

Here is

regex demo


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

Solution 2

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

Solution 3

Unquoted JSON is not really a valid JSON. It is just JavaScript. If you trust the source of this string:

var obj = eval("'({ 
time:  { 
      from:now-60d,
      mode:quick,
      to:now } 
 })'");

This is NOT recommended for strings from untrusted sources as it could be a security risk.

Given that you are getting the data from Kibana which may be trusted, it should be ok to eval the string.

The other option is to use the regex as probably elaborated by other answers. Alternatively, you may want to fix your Kibana export to give a proper/valid JSON string.

Solution 4

Good day Idriss

if you wanted to place quotes around all the valid key names and values The maybe look at this expression. YCF_L's answer is prefect to what you wanted. But here it is none the less.

{(?=[a-z])|[a-z](?=:)|:(?=[a-z])|[a-z](?=,)|,(?=[a-z])|[a-z](?=})
str.replace(/{(?=[a-z])|[a-z](?=:)|:(?=[a-z])|[a-z](?=,)|,(?=[a-z])|[a-z](?
=})/igm, $&");
Share:
11,792
Idriss Benbassou
Author by

Idriss Benbassou

BI + Developer

Updated on June 11, 2022

Comments

  • Idriss Benbassou
    Idriss Benbassou almost 2 years

    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

  • Idriss Benbassou
    Idriss Benbassou almost 7 years
    Thank you for the answer
  • Idriss Benbassou
    Idriss Benbassou almost 7 years
    Thank you for the answer
  • SamWhan
    SamWhan almost 7 years
    You're welcome. And @IdrissBenbassou - if this helped you (which it obviously did ;), shouldn't you up-vote (the up-arrow to the left of the answer)? It makes it easier for other having similar problems to find a good answer.
  • Tanzeel
    Tanzeel over 5 years
    how can i do this in bash ?
  • Youcef Laidani
    Youcef Laidani over 5 years
    Hi @Tanzeel sorry I'm not familiar with bash but you can ask a question and I'm sure you will get an answer in few minutes :)
  • Dinesh
    Dinesh over 3 years
    Great answer unfortunately. The first solution is messing up commas in the value for example => {test : "1,000.00"}, the comma in the value is being removed after normalization. And the second solution gets rid of the spaces in the value {test: "2020/11/19 16:11:46" }
  • millenion
    millenion over 3 years
    Works fine, you should add "_" character too to your regex.
  • Sri Bharath
    Sri Bharath almost 3 years
    Hi @YCF_L , This some how doesn't work if the values have , within them. Any idea on how to handle those?
  • Youcef Laidani
    Youcef Laidani almost 3 years
    @SriBharath If I understand your question, then you need to replace [a-zA-Z0-9-]+ with [a-zA-Z0-9-,]+ not that I add , to the class
  • Sri Bharath
    Sri Bharath almost 3 years
    I tried out the same. But somehow it does not follow if the json has two or more elements. For example, street:Street1,Road1 is correctly formatted to "street":"Street1,Road1". But-- street:Street1,Road1,country:USA is formatted to "street":"Street1,Road1,country":USA which is quite messed up as It considers , used to separate each element as part of the string
  • Youcef Laidani
    Youcef Laidani almost 3 years
    @SriBharath I would suggest to create a question, like this we can understand your problem and gives you the correct solution :)
  • Sri Bharath
    Sri Bharath almost 3 years
    @YCF_L Just raised an new question, stackoverflow.com/questions/68256390/… Thanks for your prompt response though :)