Jason
Jason

Reputation: 11363

Javascript - strip out occurrences of u' in JSON string, parse is returning unexpected token

I have a textfield in a database that contains the results of a python json.dumps(list_instance) operation. As such, the internal fields have a u' prefix, and break the browser's JSON.parse() function.

An example of the JSON string is

"density": "{u'Penobscot': 40.75222856500098, u'Sagadahoc': 
  122.27083333333333, u'Lincoln': 67.97977755308392, u'Kennebec': 
  123.12237174095878, u'Waldo': 48.02117802779616, u'Cumberland':  
  288.9285325791363, u'Piscataquis': 3.9373586457405247, u'Hancock': 
  30.698239582715903, u'Washington': 12.368718341168325, u'Aroostook': 
  10.827378163074039, u'York': 183.47612497543722, u'Franklin':  
  16.89330963710371, u'Oxford': 25.171240748402518, u'Somerset': 
  12.425648288323485, u'Knox': 108.48302300109529, u'Androscoggin': 
  208.75502815768303}"

What I'd like to do is replace those occurrences of u' with a '(single-quote). I've tried

function renderValues(data){
   var pop = JSON.parse(data.density.replace(/u'/g, "'"));
}

but I'm always getting a unexpected token ' exception. Since many of the possible key fields may contain a u, it is not feasable to just delete that character. How can I find all instances of u' and replace with ' without getting the exception?

Upvotes: 4

Views: 5913

Answers (4)

James Bellaby
James Bellaby

Reputation: 168

I had a similar issue and made this regex that found all of the u's even if the values had them too.

replace(/(?!\s|:)((u)(?='))/g, "")

The accepted answer, I found, missed these occurrences.

I know the OP's doesn't have 'u' for the values and only for keys but thought this may be useful too :)

Upvotes: 0

Ian Coronado
Ian Coronado

Reputation: 1

a little bit old in the answer but if there is no way to change or access the server response try with:

var strExample = {'att1':u'something with u'};

strExample.replace(/u'[\}|\,]/g, "ç'").replace(/u'/g, "'").replace(/ç'/g, "u'");

//{'att1':'something with u'};

The first replace will handle the u' that are in the trailing part of the string in the object changing it to 'ç' character

then removing the u from the phyton unicode and finally change it to u' like the original

Upvotes: 0

tenub
tenub

Reputation: 3446

Updated solution: replace(/u'/g, "'")); => replace(/u'(?=[^:]+')/g, "'"));.

Tested with the following:

"{u'Penobscot': 40.75222856500098, u'Sagadahoc': 122.27083333333333, u'Lincoln': 67.97977755308392, u'Kennebec': 123.12237174095878, u'Waldo': 48.02117802779616, u'Cumberland': 288.9285325791363, u'Piscataquis': 3.9373586457405247, u'Hancock': 30.698239582715903, u'Timbuktu': 12.368718341168325, u'Aroostook': 10.827378163074039, u'York': 183.47612497543722, u'Franklin': 16.89330963710371, u'Oxford': 25.171240748402518, u'Somerset': 12.425648288323485, u'Knox': 108.48302300109529, u'Androscoggin': 208.75502815768303}".replace(/u'(?=[^:]+')/g, "'");

results in:

"{'Penobscot': 40.75222856500098, 'Sagadahoc': 122.27083333333333, 'Lincoln': 67.97977755308392, 'Kennebec': 123.12237174095878, 'Waldo': 48.02117802779616, 'Cumberland': 288.9285325791363, 'Piscataquis': 3.9373586457405247, 'Hancock': 30.698239582715903, 'Timbuktu': 12.368718341168325, 'Aroostook': 10.827378163074039, 'York': 183.47612497543722, 'Franklin': 16.89330963710371, 'Oxford': 25.171240748402518, 'Somerset': 12.425648288323485, 'Knox': 108.48302300109529, 'Androscoggin': 208.75502815768303}"

Upvotes: 2

bgusach
bgusach

Reputation: 15175

The accepted solution is wrong. Your code fails because that string is not valid JSON. Fixing the pseudo-JSON string by replacing it is wrong.

What you have to do is fix the python code that is producing that broken JSON string, which I am pretty sure it is a str() or unicode() where there should be nothing. What you have as a value for the key "density" is a string instead of a dictionary, and therefore, python returns you something like the following:

{"density": "a string that looks like JSON but it is in fact a string reprensentation of a dictionary"}

The function json.dumps will return you always valid JSON strings.

Fix that and you will not have to hack around with filthy string replacements or whatever.

EDIT

Check the following snippet out. There you can see that the u'...' is just the python readable-representation of a unicode object, and has nothing to do whatsoever with a JSON serialization.

>>> import json
>>> data = {u'name': u'Manuel', u'age': 26}
>>> print data
{u'age': 26, u'name': u'Manuel'}  # this is the python representation of a dictionary
>>> print json.dumps(data)
{"age": 26, "name": "Manuel"} # this is a valid JSON string

That JSON is not properly formed, as easy as that.

Upvotes: 5

Related Questions