2

I have a string:

a = "{u'a':[u'123',u'321'], u'b':[u'456',u'654']}"

I want to convert this a to a json data.

How to do it?

I have tried JSON.parse(), but it will raise an error.

SyntaxError: Unexpected token u
3
  • 1
    Isn't that obvious? What is that u before the single quotes? u'a', u'b', etc.? a = "{'a':['123','321'], 'b':['456','654']}"; Commented Aug 30, 2013 at 4:47
  • What is u ? a variable ? Commented Aug 30, 2013 at 4:50
  • 1
    @Dilantha: It's the prefix for a Python unicode literal. Commented Aug 30, 2013 at 4:52

5 Answers 5

3

That looks like Python literal syntax to me. Tell whoever wrote the Python portion to encode as JSON instead of just outputting the structure as a string.

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

1 Comment

Yes. This is the best way. ;)
1

In this particular case, you can just replace each u'x' with "x" and you'll have valid JSON:

var a = "{u'a':[u'123',u'321'], u'b':[u'456',u'654']}";

// Convert to JSON (May not work with other inputs!)
var json = a.replace(/u'((?:[^'\\]|\\.)*)'/g, function(a,str){
  return '"' + str.replace(/\\'/g, '\'').replace(/"/g, '\\"') + '"'; 
});

// Parse the JSON into a Javascript object
var obj = JSON.parse(json);

Updated to work with some quotes in the object:

var a = "{u'a':[u'\\'123\\'',u'321'], u'b':[u'456\\\\',u'\"654\"']}";

Becomes:

{a:["'123'","321"], b:["456\",""654""]}

9 Comments

will not work if the strings contain unescaped double quotes, specifically
yes I have tried to do it. Like @jandvorak said how to handle single or double quotes?
@meshuai Can you show in your question what an example looks like with quotes in it?
Umm... one more thing: you need to escape double quotes. I'd use replace-with-callback for that
I said "escape double quotes", not "unescape single quotes". Though the latter needs to be done as well (sorry).
|
0

You need to change your input string to a valid JSON string.

I think this is what you wanted:

JSON.parse('{"a":["123","321"], "b":["456","654"]}')

Comments

-1

The variables (keys / values ) are strings so need to be in quotes in order to be parsed as valid JSON.

// put quotes around the key / value pairs.

var a = "{\"u'a'\":[\"u'123'\",\"u'321'\"], \"u'b'\":[\"u'456'\",\"u'654'\"]}";

jQuery

a = $.parseJSON(a);

5 Comments

how? If "by the server", why? Why not output the correct JSON right away?
also, why use $.parseJSON instead of JSON.parse? Do you need to support IE7? More importantly, who said jQuery was available?
@JanDvorak To your first question, you need to ask the OP this. I just hinted that the key/value pairs need to be in quotes. On the second question, will you use eval instead?? If not jquery, you will need to use json.js. Take your pick. I don't want to answer some of the rhetorical questions, it's a matter of choice, mine not necessarily be the same as yours.
my point is that OP did not say he needed IE7 support, and he did not say he was using jQuery. By the way, guess what jQuery uses as a fallback? ;-)
yes, it does. A matter of habit, i guess. your points are valid though.
-1

JSON is supposed to contain only strings.

a = "{u'a':[u'123',u'321'], u'b':[u'456',u'654']}"

Here I can only assume that u is a variable. Change it to this:

a = "{"+u+"'a':["+u+"'123',"+u+"'321'], "+u+"'b':["+u+"'456',"+u+"'654']}"

5 Comments

Interesting you thought that. I assumed it was a string prefix and should have been omitted.
Its possible. A prefix can be included in the string. Only reason not to would be if it was a variable.
Even if your interpretation was correct, a is still not valid JSON. It's not even valid Javascript code
now it's just that your interpretation is incorrect ;-) See the other answers as well as question comments. Concatenating a variable to all JSON keys hardly makes any sense. Also, is there any value of u (except "") that produces a valid JSON?
Yeah I did read them, but this is what I thought when I first saw it.

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.