-2

i have this string [['18', '9'], ['18', '9'], ['18', '9'], ['18', '9'], ['18', '9'], ['18', '9']]

so I test all the ways to find out how to convert it to a list or a JSON format in python

when i try to convert it using json.loads()

it shows error

items = '[['18', '9'], ['18', '9'], ['18', '9'], ['18', '9'], ['18', '9'], ['18', '9']]'
item = json.loads(items)

and the error is

json.decoder.JSONDecodeError: Expecting value: line 1 column 3 (char 2)
Barmar
  • 596,455
  • 48
  • 393
  • 495

2 Answers2

3

That's not JSON, it's Python syntax. Use ast.literal_eval() to parse it.

import ast

items = "[['18', '9'], ['18', '9'], ['18', '9'], ['18', '9'], ['18', '9'], ['18', '9']]"
result = ast.literal_eval(items)
Barmar
  • 596,455
  • 48
  • 393
  • 495
1

Your quoting wrong, this works:

>>> import json
>>> items = '[["18", "9"], ["18", "9"], ["18", "9"], ["18", "9"], ["18", "9"], ["18", "9"]]'
>>> item = json.loads(items)
>>> print(item)
[['18', '9'], ['18', '9'], ['18', '9'], ['18', '9'], ['18', '9'], ['18', '9']]
yvesonline
  • 3,583
  • 2
  • 15
  • 26