5

I have below javascript method. I try to get the id from the data parameter.

data(spark, data){
    console.log('receive ', data)
    console.log(data.id)
  }

the first output line is receive {id:1}

but the second output line is undefined

then I tried below method to convert the json string to object:

data(spark, data){
    console.log('receive ', data)
    console.log(JSON.parse(JSON.stringify(data)).id)

I still got the same output. Why can't I get the id from the input parameter?

EDIT1

I changed the parameter name to be different with the function name as below:

data(spark, d){
    console.log('receive ', d)
    console.log(JSON.parse(JSON.stringify(d)).id)
}

but I still got the same output.

13
  • 3
    Note that id should be wrapped in double quotes, then you can use var json = JSON.parse(data); console.log(json.id); Commented Dec 20, 2016 at 4:57
  • 1
    Why do you want to have the function name and variable name the same? Commented Dec 20, 2016 at 4:58
  • 1
    This won't work. You are using both the function name and variable name as the same string. For this kind of situation, you can do an eval(), but this is not at all advised. Commented Dec 20, 2016 at 4:58
  • 1
    Agreeing with Praveen, I'd recommend either changing the name of your argument, or changing the name of your function. It's very ambiguous as to what data could mean in your code, and it's better to err on the side of caution. Commented Dec 20, 2016 at 5:00
  • 2
    Maybe you can consider to change the way you get this string? Commented Dec 20, 2016 at 5:08

3 Answers 3

2

Note: Strong word of caution. Just checking if this works, and it works. Do not use it in unexpected circumstances. Very dangerous!

One crazy thing is, you forgot the function keyword, in-front of the function name.

Trying with eval() for this:

function data(spark, d) {
  // console.log('receive ', d);
  eval("d = " + d);
  console.log(d.id);
}
data("", "{id: 5}");

Sign up to request clarification or add additional context in comments.

8 Comments

My function is defined inside a class (es6), so there is no function keyword. I tried your approach it works. but it is dangerous. Is there any other way to do that?
With your current implementation, I am not sure if there's a valid way.
Why doesn't JSON.parse work in this case?
The correct "solution" to this question is to have the server send down correctly formed JSON.
@torazaburo Agreed... What if the server is not under the OP's control?
|
0

Four answers:

  1. Have the server send down properly formed JSON (with double quotes around the key names). This is the preferred solution.

  2. Use a relaxed JSON parser. You can search and find such things on npm.

  3. Use eval.

  4. Do some string manipulation to enclose the id in double quotes so you can JSON.parse it, or extract the 1, or whatever you want to do. This is the least preferred solution.

10 Comments

1. What if the server is not under control? 2. NPM? Seriously?
I'm not recommending using a relaxed/dirty parser, but I think it's better than eval. What is your problem with npm? Where else would you go looking for one?
I thought NPM is only for Node? Am I wrong?
Easy enough to use node packages in the browser with something like browserify or webpack.
And the crazy part is, you asked me not to use and you have added eval() as well.
|
-2

eval("(" + data + ")").id is all that is required

function data(spark, data) {
  alert(eval("(" + data + ")").id);
}

data("", "{id:3}");

4 Comments

I tried but got the same output
This will work if data is {"id":1} but data is {id:1} so JSON.parse will throw an error
@slebetman try it, this will work
Can the people who downvoted this answer please explain their reasons. I can't see what is wrong with it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.