Whats wrong with this JSON syntax? - javascript

Am trying this on my chrome debugger console, and am getting a SyntaxError;
JSON.parse("[{"name":"gath","age":10}]");
>SyntaxError
What is the correct way of parsing the JSON string?
Please note this question is a follow up to my earlier one, which am yet to get an answer!

You need to escape your double-quotes.
JSON.parse("[{\"name\":\"gath\",\"age\":10}]");
or, for better readabilty, define the string with single quotes:
JSON.parse('[{"name":"gath","age":10}]');

JSON.parse("[{\"name\":\"gath\",\"age\":10}]");
You cant have double quotes inside double quotes

You need to escape the "
or do JSON.parse('[{"name":"gath","age":10}]');

Enclose it in single quotes and it will parse correctly.
JSON.parse('[{"name":"gath","age":10}]');
Object
age: 10
name: "gath"
__proto__: Object

Replace
JSON.parse("[{"name":"gath","age":10}]");
With
JSON.parse('[{"name":"gath","age":10}]');

Related

How to make string to json object

I have below string
'[{\'Question\': \'a Names and Roles (if known)\'}]'
I need to convert it into JSON.
I tried JSON.parse(s)
I got error SyntaxError: Unexpected token ' in JSON at position 2
and also
> eval(s)
SyntaxError: Unexpected string
Any help would be really appreciable
Problem is that you need to pass string inside JSON.parse(s).
Please make sure s is a string
'[{*Question\': at the point of asterisk you need a \' don't you?
So it should be
'[{\'Question\': \'a Names and Roles..
EDIT
Found this after a bit of research. Problem is with the single quote. Replace them with double quotes, you are good to go. See this fiddle, to check this out in action.
Additionally, I had to modify "[\'NA\']" in your string, to get it to working. That is also invalid JSON. If it is an array that you want it to be, you should put it like [\"NA\"].

Pass image url from variable to html in javascript

In my script, text(message.image) returns a dynamic URL path of an image. What would be the correct statement in a javascript?
This fails:
$('<div/>').'<img src=\"'.text(message.image).'\">'.appendTo($('#msgDiv'));
The real answer is:
$('<div><img src="'+message.image+'"/></div>').appendTo($('#msgDiv'));
You have a couple syntactic errata in your code snippet:
You can't access a property with a string like you do.
Concatenation of strings is not with a dot but with a plus.
You are trying to execute text() on a string, not on the div.
I think you're missing (),+,append method and escaping " incorrectly, try with this:
$('<div/>').append('<img src="' + text(message.image) + '"/>').appendTo($('#msgDiv'));
Hope this helps,
I think you have a problem because you're using (double)quote badly
You escape the double quote but you're using simplequote. Try that :
$('<div/>').'<img src=\''.text(message.image).'\'>'.appendTo($('#msgDiv'));
And are you sure concerning the syntax $('').'SOME HTML CODE' ?
You are missing ( and ) there.
Also, you dont need to escape double quotes since you are using single quotes outside.
Try this:
$('<div/>').('<img src="'+text(message.image)+'">').appendTo($('#msgDiv'));

alerting double encoded character

I'm trying to alert document.cookie when the dot is double encoded, but it doesn't work.
here's my code:
<script>alert(unescape(document%252Ecookie))</script>
How can i make it work using double encoding on the dot?
Thanks.
No idea what you are trying to do really, but here are some clarifications:
unescape() works on strings. it takes a string and returns another string. document%252Ecookie is not a string, so you cannot use unescape() on it.
To use unescape, put quotes around "document%252Ecookie"
unescape(unescape("document%252Ecookie")) will give you the string "document.cookie" (remember, unescape goes from strings to strings). If you alert this you will have an alert box saying "document.cookie", which is probably not what you are looking for. To get the contents of document.cookie you can use eval(). It takes a string and evaluates the contents of it.
So to do what you want to do you would type:
alert(eval(unescape(unescape("document%252Ecookie"))))
No idea of why you would want to do this, but this is how it can be done. I want to point out that this code is pretty stupid, but hopefully I've made some things more clear to you.

JSON.stringify doesn't escape?

I'm using `JSON.stringify? to stringify an object, but the quotes are not escaped? Am I misunderstanding that it's suppose to escape the quotes?
This is outputted into the template without any of the quotes being escaped:
{"console":{"free":false}}
stringify the object twice does the trick
console.log(JSON.stringify(JSON.stringify({"console":{"free":false}})));
// "{\"console\":{\"free\":false}}"
It doesn't escape characters, no, there's encodeURIComponent for that, and you can use them together, as in encodeURIComponent(JSON.stringify(obj))
The quotes around property names are not supposed to be escaped, only quotes inside strings. Your JSON is fine :)
Without the offending code to inspect, I'm wondering if something else is happening. As a test...
<div id="test"/>
var ex = {'test':'This is "text".'};
$('#test').text(JSON.stringify(ex));
Outputs: {"test":"This is \"text\"."} (< Note the escaped double quotes)
http://jsfiddle.net/userdude/YVGbH/

JSON String as Javascript function argument

I am trying to define a pure JSON string as an argument in a Javascript function.
Below is the way I want to define it:
Link
Firebug gives me an error alert: unterminated string literal, even when I escape the double-quotes on the JSON string.
How can I solve this?
Thanks.
Use " for your double quotes, then in js_func(), replace them with actual double quote characters (") before evaluating your JSON string. (thanks for the demo Matthew, I updated your fiddle with the example from the question:)
http://jsfiddle.net/brillyfresh/kdwRy/1/
simply defining the link as Link works fine. JSON is valid JavaScript, you don't need to enclose it in ''s.
I also suggest to use an EventListener (element.addEventListener()), this makes the html cleaner and would reduce this problem to nothing.
ryou are either trying to pass the parsed object or pass a string
Object: onclick="js_func(arg_1, arg_2, {'key': 'value'});"
String: on_click="js_func('arg_1', 'arg_2', '{\"key\": \"value\"}'); return false"
All I've got handy to test is firebug interpreter but both worked fine for me.
>>>>'{\"key\": \"value\"}'
"{"key": "value"}"
>>>> {'key': 'value'}
Object {key="value"}
(I don't mean to presume whether arg_1 and arg_2 are strings or variable names, and it doesnt matter. just did the same thing as with the JSON)

Categories

Resources