0

I have a list which is in single quotes.

'[{"Name":"name1","value":"value1"},{"name":"name2","value":"value2"}]'

I receive this as an input parameter and I would like to assign this to a variable without the enclosing single quotes in python.

Could you please help?

The sample data received is close to something below..

 '[{"name":"emp_id","value":3232323},{"name":"dd_approval","value":"-paid -fulltime \"05/03/21 19:46\" \"05/04/21 19:46\" xyz@dmuil.com 3232323"}]'
 json.loads(this string)  

Gives an error as given below

File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
  • That's not a list, but a string – azro May 04 '21 at 20:53
  • This is a *string*. It doesn't have enclosing single quotes, that is how the printed `repr` would show, or if it were a string literal in source code. You are trying to *convert this to a list*. It looks like this is JSON, so just use the `json` module – juanpa.arrivillaga May 04 '21 at 20:54

2 Answers2

0

What you have is not a list, but a JSON string representing a list of object, loading it in python will result in a list of dict

import json
a = '[{"Name":"name1","value":"value1"},{"name":"name2","value":"value2"}]'
content = json.loads(a)
print(content) # [{'Name': 'name1', 'value': 'value1'}, {'name': 'name2', 'value': 'value2'}]
Red
  • 26,798
  • 7
  • 36
  • 58
azro
  • 53,056
  • 7
  • 34
  • 70
  • This question is still open. Not sure why people are always in a hurry to close a question without giving it time atleast for a few hours. – Python Charmer May 05 '21 at 01:59
  • @PythonCharmer If a question is a duplicate let's avoid to spread the knowledge. Because a poor answer can be accepted, then all people comming here won't read the good answer in the duplicate link – azro May 05 '21 at 07:39
  • Agreed but sometimes same type of question can have multiple answers due to multiple situations for ex: platform dependency, version and so on. We must wait for a few hours before marking it duplicate and also ask the person who posted if the duplicate link answered his question before marking it as closed. We should not jump the queue too soon. I am pretty sure most users take a look at the duplicate questions nd post only when they dont find the answer they need. With best regards!!! – Python Charmer May 07 '21 at 09:50
0

You can use ast.literal_eval

import ast
x = ast.literal_eval('[{"Name":"name1","value":"value1"},{"name":"name2","value":"value2"}]')

If I preform type() on x I will be presented with <class 'list'>

Also everything inside this list turns to its desired form. The code below proves just that.

import ast
x = ast.literal_eval('[{"Name":"name1","value":"value1"},{"name":"name2","value":"value2"}]')
print(x[0])
print(type(x[0]))

output

{'Name': 'name1', 'value': 'value1'}
<class 'dict'>
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44