0

I have an a string like the following,

var x = "[{"k":"1"}]";
console.log(x[0].K);

I ant hange this string sine I am getting from the server.Its out of my control. I want to display the 'k' value and when I onsole it,got

  Uncaught SyntaxError: Unexpected identifier

I know,it beasuse of the 'double quotes' inside and outside of the array.

It is not even allowing me to parse it.

So,I thought

1)Replace "[  and ]" as '[  and ]'  so it becomes

 '[{"k":"1"}]'

2)Then I can parse and get the 'k' value.

var x = "[{"k":"1"}]";
console.log(x.replace(/"[/g," '[ ").replace(/"]"/g," ]'"));

But still I am getting the same unexpeted identifier error,an anyone please suggest me help.Thnaks.

Testimg198
  • 55
  • 1
  • 2
  • 8
  • This is JSON, so: [Safely turning a JSON string into an object](https://stackoverflow.com/q/45015/218196) – Felix Kling Jun 13 '18 at 00:00
  • How are you getting this string? – Felix Kling Jun 13 '18 at 00:01
  • Probably just a typo, but you'll need to be case sensitive. .k and .K are two different things. – Ryan Gibbs Jun 13 '18 at 00:01
  • @Ryan,edited my ode. – Testimg198 Jun 13 '18 at 00:04
  • *"So,I thought...."* This cannot work because the browser cannot even execute the code. You are not getting the error for trying to access the property. You are getting it because your code literally contains `"[{"k":"1"}]"`. If you'd tell us how that strings gets into your code we could help you better. Please read [ask]. – Felix Kling Jun 13 '18 at 00:04
  • Take a look at https://codepen.io/anon/pen/MXmMvL?editors=1112, is this what you want? be sure to open the console. – Pheonix2105 Jun 13 '18 at 00:13
  • @Flex,I am getting that strings from server side and the db onatins the data like that whih is out of my control to change it. – Testimg198 Jun 13 '18 at 00:27
  • @Pheonix,yes but please consider the string value like this,var jsonData = '[{"name" : "Apple"}, {"name" : "Pear"} ]'; – Testimg198 Jun 13 '18 at 00:28
  • Update your question with a sample of the JSON data you get sent by the server, simply do console.log(x) and paste the results into your question to give us an idea of how your data is structured. I think I understand your question now but I need to see what data you get sent. – Pheonix2105 Jun 13 '18 at 00:29
  • You are saying the DB stores the data **with** the outer double quotes? – Felix Kling Jun 13 '18 at 04:44

4 Answers4

8

You're still using double quotes around x.

var x = '[{"k":"1"}]';

Use JSON.parse to turn it into an array:

var array = JSON.parse(x);
console.log(array); // [{ "k": "1" }]
Christian Scott
  • 893
  • 7
  • 19
  • Hi Christian,beause of double quotes its not allowing me to parse. – Testimg198 Jun 13 '18 at 00:02
  • @Testimg198: You are providing too little information for us to help you effectively. You cannot have the character sequence `"[{"k":"1"}]"` in your code. It's just not possible. You have to fix that. But since you are not telling us how that character sequence gets into your code we cannot really help you. You either have to edit the file with your text editor and change the outer double quotes to single quotes, or if the code is generated/embedded somehow, update the process that does that. – Felix Kling Jun 13 '18 at 00:07
3

Does it need to an array of objects containing strings?

It seems you are over complicating it, try

//create a javascript object as x, set the VALUE (1) of KEY (k)
var x = {"k":"1"};
//log the objects k property
console.log(x.k);

Just realised what you are trying to achieve, go with the below answer if you're trying to parse JSON that's echo'd via PHP or something similar.

Example parsing JSON to a javascript array

var jsonData = '[{"name" : "Apple"}, {"name" : "Pear"} ]';

var parsedJson = JSON.parse(jsonData);
console.log(parsedJson[0]);

parsedJson.forEach(function(fruit){
  console.log(fruit.name);
});
Pheonix2105
  • 1,001
  • 2
  • 9
  • 24
  • *"if you're trying to parse JSON that's echo'd via PHP"* Even then it would be better to embed it as an object literal, not as a string. – Felix Kling Jun 13 '18 at 00:01
0

Use String.prototype.slice to strip the outer double quotes from the string returned by the server.

Then use JSON.parse:

var x = `"[{"k":"1"}]"`
var x2 = x.slice(1,-1);
var x3 = JSON.parse(x2);
console.log(x)
console.log(x2);
console.log(x3[0]);
console.log(x3[0].k);
georgeawg
  • 48,608
  • 13
  • 72
  • 95
0

I've been doing JSON with double quotes and found this solution:

var x = "[{\"k\":\"1\"}]";
var parsedx = JSON.parse(x);
console.log(parsedx[0].k);

By using \" it will cancel those double quotes until it has been parsed.

Nick is tired
  • 6,860
  • 20
  • 39
  • 51