SJ-B
SJ-B

Reputation: 95

Javascript : Replace Backslash and double Quotes with some other char

I am trying to replace \" with null or some other character,and trying to JSON parse but its not happening at al. Below is my code.

var s = '{"total":2,"data":[{"assignExist":"Online","status":"Started Work","state":"Ready for Download","assignInfoletId":"100003","loadAssignId":"false","assignAdditionalInfo":[{}],"assignName":"BC Simple Form","assignId":"100003","assignProcessId":"3"},{"assignExist":"Online","status":"Started Work","state":"Ready for Download","assignInfoletId":"100003","loadAssignId":"false","assignAdditionalInfo":[{"Name":"Rishant","City":"Bangalore","Desc":"Platform-Dev","AUDIT_COMPANIES":"[{\"AUDIT_COMPANY\":\"Golder\",\"AUDITORS\":[{\"TPA_AUDITOR\":\"Swadhin Ray\",\"IS_LEAD\":\"0\",\"ASSIGNMENTS\":\"Assigmens, Sdsfds,sdfdsfd\"},{\"TPA_AUDITOR\":\"Praveen Kayetha\",\"IS_LEAD\":\"1\",\"ASSIGNMENTS\":\"Assigmens, Sdsfds,sdfdsfd\"}]}]"}],"assignName":"BC Simple Form","assignId":"100001","assignProcessId":"2"}]}';


s= s.replace(/\\"/g, "DOUBLEQ");

console.log(s);
var data = JSON.parse(s);
console.log(data);

solution to this i found is

var dataResponse = JSON.parse(s);

      var dataLen = dataResponse.data.length;

      for(var i=0;i<dataLen;i++){
        var audit_co = dataResponse.data[i].assignAdditionalInfo[0].AUDIT_COMPANIES;
        if(typeof audit_co!= 'undefined')
          dataResponse.data[i].assignAdditionalInfo[0].AUDIT_COMPANIES = JSON.parse(audit_co.replace(/\\"/g,'"'));
      }

Upvotes: 0

Views: 570

Answers (5)

ckck
ckck

Reputation: 11

The following method should work:

var s = JSON.stringify({"total":2,"data":[{"assignExist":"Online","status":"Started Work","state":"Ready for Download","assignInfoletId":"100003","loadAssignId":"false","assignAdditionalInfo":[{}],"assignName":"BC Simple Form","assignId":"100003","assignProcessId":"3"},{"assignExist":"Online","status":"Started Work","state":"Ready for Download","assignInfoletId":"100003","loadAssignId":"false","assignAdditionalInfo":[{"Name":"Rishant","City":"Bangalore","Desc":"Platform-Dev","AUDIT_COMPANIES":"[{\"AUDIT_COMPANY\":\"Golder\",\"AUDITORS\":[{\"TPA_AUDITOR\":\"Swadhin Ray\",\"IS_LEAD\":\"0\",\"ASSIGNMENTS\":\"Assigmens, Sdsfds,sdfdsfd\"},{\"TPA_AUDITOR\":\"Praveen Kayetha\",\"IS_LEAD\":\"1\",\"ASSIGNMENTS\":\"Assigmens, Sdsfds,sdfdsfd\"}]}]"}],"assignName":"BC Simple Form","assignId":"100001","assignProcessId":"2"}]});

var data = JSON.parse(s);

Upvotes: 0

Tomalak
Tomalak

Reputation: 338158

Currently you have this:

var s = '{"plainValue": 42, "nestedJSON": "{\"nested\": \"json\"}"}';

This is not valid because it translates to the following in-memory string:

{"plainValue": 42, "nestedJSON": "{"nested": "json"}"}
                                   ^      ^  ^    ^     invalid double quotes

The solution is simple: Don't use nested JSON. There is no reason whatsoever to nest a JSON string into a JSON string. Here is what you should have.

var s = '{"plainValue": 42, "nestedObject": {"nested": "object"}}';

Syntactically valid but foolish would be

var s = '{"plainValue": 42, "nestedJSON": "{\\"nested\\": \\"json\\"}"}';

Fix the problem at the source. The function that generates your JSON string is broken.

Upvotes: 1

Vicky Gonsalves
Vicky Gonsalves

Reputation: 11707

You dont need to parse it to invalid JSON:
You can access values directly:

<script>
    var s = {"total":2,"data":[{"assignExist":"Online","status":"Started Work","state":"Ready for Download","assignInfoletId":"100003","loadAssignId":"false","assignAdditionalInfo":[{}],"assignName":"BC Simple Form","assignId":"100003","assignProcessId":"3"},{"assignExist":"Online","status":"Started Work","state":"Ready for Download","assignInfoletId":"100003","loadAssignId":"false","assignAdditionalInfo":[{"Name":"Rishant","City":"Bangalore","Desc":"Platform-Dev","AUDIT_COMPANIES":"[{\"AUDIT_COMPANY\":\"Golder\",\"AUDITORS\":[{\"TPA_AUDITOR\":\"Swadhin Ray\",\"IS_LEAD\":\"0\",\"ASSIGNMENTS\":\"Assigmens, Sdsfds,sdfdsfd\"},{\"TPA_AUDITOR\":\"Praveen Kayetha\",\"IS_LEAD\":\"1\",\"ASSIGNMENTS\":\"Assigmens, Sdsfds,sdfdsfd\"}]}]"}],"assignName":"BC Simple Form","assignId":"100001","assignProcessId":"2"}]};
    console.log(s.data);
</script>

Upvotes: 1

Andy1210
Andy1210

Reputation: 144

when you create the 's' string, the \" is already esaped, so the \ char is dissapear. Thats why you cant replace \" with something, because its doesn't exist.

Upvotes: 0

Robin Green
Robin Green

Reputation: 33033

I don't think it's illegal JSON - it looks like JSON inside JSON, which is valid. Try just parsing it without changing it.

Upvotes: 1

Related Questions