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

javascript - Error parsing JSON with escaped quotes inside single quotes - Stack Overflow

programmeradmin2浏览0评论

I have a variable var jsonData = '{"Key":"query","Value":"dept=\"Human Resources*\"","ValueType":"Edm.String"}';

I'm trying to parse the variable with JSON.parse(jsonData), however, I'm getting an error "Unexpected token H in JSON at position 30." I can't change how the variable is returned, so here's what I think I understand about the problem:

  1. The JSON.parse(jsonData) errors out because it's not recognizing the escaped double quotes as escaped since it is fully enclosed in single quotes
  2. jsonData.replace(/\\"/g, "\\\\"") or other binations that I've tried aren't finding the \" because javascript treats \" as just "

QUESTION How can I parse this properly, by either replacing the escaped quotes with something JSON.parse() can handle or using something else to parse this correctly? I'd like to stick with JSON.parse() on account of it's simplicity, but open to other options.

EDIT: Unfortunately I can't change the variable at this stage, it is just a small example of a larger JSON response. This is a temporary solution until the app is granted access to the API, but I needed the solution in the interim until that happens (IT dept can be slow). What I'm doing now its getting a large JSON response back by hitting the API address directly and the browser uses the cookies from the user OAuth for authentication. I then copy and paste the JSON response into my application so I can work with the data. The response is riddled with the escaped quotes and manually editing the text would be laborious and I'm trying to avoid copying into text processor before copying into the variable.

I have a variable var jsonData = '{"Key":"query","Value":"dept=\"Human Resources*\"","ValueType":"Edm.String"}';

I'm trying to parse the variable with JSON.parse(jsonData), however, I'm getting an error "Unexpected token H in JSON at position 30." I can't change how the variable is returned, so here's what I think I understand about the problem:

  1. The JSON.parse(jsonData) errors out because it's not recognizing the escaped double quotes as escaped since it is fully enclosed in single quotes
  2. jsonData.replace(/\\"/g, "\\\\"") or other binations that I've tried aren't finding the \" because javascript treats \" as just "

QUESTION How can I parse this properly, by either replacing the escaped quotes with something JSON.parse() can handle or using something else to parse this correctly? I'd like to stick with JSON.parse() on account of it's simplicity, but open to other options.

EDIT: Unfortunately I can't change the variable at this stage, it is just a small example of a larger JSON response. This is a temporary solution until the app is granted access to the API, but I needed the solution in the interim until that happens (IT dept can be slow). What I'm doing now its getting a large JSON response back by hitting the API address directly and the browser uses the cookies from the user OAuth for authentication. I then copy and paste the JSON response into my application so I can work with the data. The response is riddled with the escaped quotes and manually editing the text would be laborious and I'm trying to avoid copying into text processor before copying into the variable.

Share Improve this question edited May 29, 2019 at 14:03 mystackoverflow1212 asked May 28, 2019 at 22:06 mystackoverflow1212mystackoverflow1212 1572 silver badges10 bronze badges 2
  • 1 Where is the JSON actually ing from? The correct course of action would be to fix it at the source – Phil Commented May 28, 2019 at 23:10
  • 1 It's not clear if you have control over the value of that variable or not. The answer will change depending on that. The way you wrote your code example, you could simply change the value of the string per the answer by Max Meijer. – Todd Chaffee Commented May 29, 2019 at 1:35
Add a ment  | 

3 Answers 3

Reset to default 3

You should escape the backslash character in your code by prefixing it with another backslash. So the code bees:

var jsonData = '{"Key":"query","Value":"dept=\\"Human Resources*\\"","ValueType":"Edm.String"}';

The first backslash is so that JS puts the second backslash in the string, which must be in the string so that the json parser knows that it should ignore the quote character.

The unfortunate thing about this situation is that in the JavaScript code there is no difference between

var jsonData = '{"Key":"query","Value":"dept=\"Human Resources*\"","ValueType":"Edm.String"}'

and

var jsonData = '{"Key":"query","Value":"dept="Human Resources*"","ValueType":"Edm.String"}'.

You could hardcode information you have about the JSON into the way you program it. For example, you could replace occurences of the regex ([\[\{,:]\s+)\" by $1\" but this would fail to work if the string Human Resources* could also end in a :, { or ,. This would also potentially cause security issues.

In my opinion, the best way to solve your problem would be to put the json response in a json file somewhere so that it can be read into a string by the javascript code that needs to use it.

I think you can also dispense with the initial String to represent the JSON object:

  1. Use a standard JSON object.
  2. Make whatever changes you need on that object.
  3. Call JSON.stringify(YOUR_OBJECT) for a String representation.
  4. Then, JSON.parse(…) when you need an object again.

That should be able to satisfy your initial request, question, keep your current (escaped) String values, and give you some room to make a lot of changes.

To escape your current String value:

  1. obj["Value"] = 'dept=\"Human Resources*\"'

Alternatively, you can nest attributes:

  1. obj["Value"]["dept"] = "Human Resources*"

Which may be helpful for other reasons.

I've found that I've rarely worked with JSON in an enterprise or production environment where the above sequence wasn't used (I've never used a purely string representation in a production environment) simply due to the ease of modifying attributes, generating dynamic data/modifying the JSON object, and actually using the JSON programmatically.

Using string representations for what are really attribute key-value pairings often causes headaches later on (for example, when you want to read the Human Resources* value programmatically and use it).

I hope you find that approach helpful!

发布评论

评论列表(0)

  1. 暂无评论