0

I am still on python 2.7. I have a list of list in which I have some strings with double quotes. Below is what I have

new_row = [['<a href=/account_dtls/risks/edit/ACC-RISK-136053>ACC-RISK-136053</a>', 'Customer_152', 'External', 'Geographic / Diplomatic', u'Account', u'<span title=something "was" okay>something "was" okay</span>', 'gowri.subramanian', 'Open', 'saved', 'Client Location']]

I wish to convert it into a json. I follow the below steps in python interpreter in terminal

<type 'list'>
>>> json.dumps(new_row)
'[["<a href=/account_dtls/risks/edit/ACC-RISK-136053>ACC-RISK-136053</a>", "Customer_152", "External", "Geographic / Diplomatic", "Account", "<span title=something \\"was\\" okay>something \\"was\\" okay</span>", "gowri.subramanian", "Open", "saved", "Client Location"]]'
>>>

that gave me a neat json string with double quotes escaped. But my problem is I am running DJANGO and within it, I convert the same list of list into json and I get the below printed in my log file.

[["<a href=/account_dtls/risks/edit/ACC-RISK-136053>ACC-RISK-136053</a>", "Customer_152", "External", "Geographic / Diplomatic", "Account", "<span title=something \"was\" okay>something \"was\" okay</span>", "gowri.subramanian", "Open", "saved", "Client Location"]]

Notice that the double quotes around the word 'was' were not escaped with double backslashes and instead with single backslash. I pass this to front-end and I try to read it using JSON.parse() but it throws below error

Uncaught SyntaxError: Unexpected token '&'

When I checked it was like this

[[&quot;&lt;a href=/account_dtls/risks/edit/ACC-RISK-136053&gt;ACC-RISK-136053&lt;/a&gt;&quot;, &quot;Customer_152&quot;, &quot;External&quot;, &quot;Geographic / Diplomatic&quot;, &quot;Account&quot;, &quot;&lt;span title=something \&quot;was\&quot; okay&gt;something \&quot;was\&quot; okay&lt;/span&gt;&quot;, &quot;gowri.subramanian&quot;, &quot;Open&quot;, &quot;saved&quot;, &quot;Client Location&quot;]]

So I tried replacing the &quot with "

replace(/&quot;/g,'"') But now I get the below error. VM86:1 Uncaught SyntaxError: Unexpected token w in JSON at position 164

I am totally frustrated here. Can someone please help me what is going on here? and how shall I fix this?

Jao
  • 558
  • 2
  • 12
Velu narasimman
  • 543
  • 1
  • 5
  • 18

1 Answers1

0

ast.literal_eval() might help you as described here.

r0w
  • 155
  • 1
  • 13