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.

10
  • This is JSON, so: Safely turning a JSON string into an object Commented Jun 13, 2018 at 0:00
  • How are you getting this string? Commented Jun 13, 2018 at 0:01
  • Probably just a typo, but you'll need to be case sensitive. .k and .K are two different things. Commented Jun 13, 2018 at 0:01
  • @Ryan,edited my ode. Commented Jun 13, 2018 at 0: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 How to Ask. Commented Jun 13, 2018 at 0:04

4 Answers 4

7

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" }]
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Christian,beause of double quotes its not allowing me to parse.
@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.
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);
});

1 Comment

"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.
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);

4 Comments

Why are the back traes '`' added to the string since I dont have them.
It simulates the string that comes from the server.
I think they meant your use of back ticks instead of single quotes, which is a feature of ECMA6, I believe?
I believe the backticks are valid and do not warrant an answer edit. Reference: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.