最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How To escape backslash in JSON object? - Stack Overflow

programmeradmin4浏览0评论

I send a POST Request with JSON Objet in body to an application which is expecting this kind of value:

{
   "input": {
      "queryFilter": "mail eq \"[email protected]\""
   }
}

The Request works fine and I get a response 200.

Then I want to send the same Request via UI/Form and try to pass it as JSON Object as following:

input: {
    queryFilter: '"mail eq \"' + mail + '\" "'
}

Which ends in: {input: {queryFilter: ""mail eq "[email protected]" ""}}

This is syntactically not correct and not as the working JSON via Postman. Also trying it as:

input: {
    queryFilter: {
       mail: mail
    }
}

didn't help.

I have been checking some postings like: How to escape Backslash in Javascript object literal, but they look handling other kind of issues So, how would it be possible to build the JSON Objet in the JavaScript file correctly so that it ends up exactly like the one working on Postman?

I send a POST Request with JSON Objet in body to an application which is expecting this kind of value:

{
   "input": {
      "queryFilter": "mail eq \"[email protected]\""
   }
}

The Request works fine and I get a response 200.

Then I want to send the same Request via UI/Form and try to pass it as JSON Object as following:

input: {
    queryFilter: '"mail eq \"' + mail + '\" "'
}

Which ends in: {input: {queryFilter: ""mail eq "[email protected]" ""}}

This is syntactically not correct and not as the working JSON via Postman. Also trying it as:

input: {
    queryFilter: {
       mail: mail
    }
}

didn't help.

I have been checking some postings like: How to escape Backslash in Javascript object literal, but they look handling other kind of issues So, how would it be possible to build the JSON Objet in the JavaScript file correctly so that it ends up exactly like the one working on Postman?

Share Improve this question edited Nov 10, 2017 at 12:25 k.vincent asked Nov 9, 2017 at 13:26 k.vincentk.vincent 4,1738 gold badges43 silver badges85 bronze badges 6
  • 1 remove the double quote like : input: {queryFilter: 'mail eq \"' + mail + '\" '} – 3Dos Commented Nov 9, 2017 at 13:30
  • It ends up as: queryFilter:"mail eq "[email protected]" " which is not expected by the application. It should keep the backslashes. e.g. "queryFilter": "mail eq \"[email protected]\"". Maybe the titel of my question isn't 100% correct for the issue. – k.vincent Commented Nov 9, 2017 at 13:34
  • In your first example, there will be no backslash characters in the resulting string. The value of that queryFilter property will be the string mail eq "[email protected]". The JavaScript parser itself will remove those backaslashes. Really, there's no such thing as a "JSON Object"; what you're building is a JavaScript object. When you serialize that with JSON.stringify() you'll get a valid JSON string. – Pointy Commented Nov 9, 2017 at 13:36
  • Why are you building JSON using string concatenation? There's a method specifically for building JSON: JSON.stringify() – JLRishe Commented Nov 9, 2017 at 13:36
  • @Pointy: Yes, true. It's being build as JavaScript Object, then being serialized to JSON. Just missed it when thinking/typing too quick. So, how to get a JSON Object at the end with a value containing backslash / and ". The Backend application is java based. – k.vincent Commented Nov 9, 2017 at 13:43
 |  Show 1 more ment

2 Answers 2

Reset to default 2

The error here is not the double quote escaping with backslashes but the actual double quotes wrapping your json value.

Remove them like the following and you should be good to go !

const mail = '[email protected]'
const obj = {
  input: {
    queryFilter: 'mail eq "' + mail + '" '
  }
}

console.log(JSON.stringify(obj))

try this :

var mail = "[email protected]";
var mailValue = "mail eq " + "\\\"" + mail + "\\\"";
var result = JSON.stringify(Object.create(null, {
  input: {
    value: {
      queryFilter: mailValue
    },
    enumerable: true
  }
}));

console.log(result);

发布评论

评论列表(0)

  1. 暂无评论