0

I have an output somewhat of the form below. How do I turn it into a dictionary or pandas indexed 2D array?

'{"key1":true,"key2":[1,1,1],"key3":[[["name1",0],["name2",0]],[[1,0][1,0][1,0]]}'
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709

2 Answers2

1

You can try this:

import ast
s = '{"key1":true,"key2":[1,1,1],"key3":[[["name1",0],["name2",0]],[[1,0],[1,0],[1,0]]}'
final_data = ast.literal_eval(s)
Ajax1234
  • 66,333
  • 7
  • 57
  • 95
0

There are missing commas in the last value.

[[1,0][1,0][1,0]]]}'

This should be:

[[1,0],[1,0],[1,0]]]}'

When fixed, you get:

>>> import json
>>> foo = '{"key1":true,"key2":[1,1,1],"key3":[[["name1",0],["name2",0]],[[1,0],[1,0],[1,0]]]}'
>>> json.loads(foo)
{u'key3': [[[u'name1', 0], [u'name2', 0]], [[1, 0], [1, 0], [1, 0]]], u'key2': [1, 1, 1], u'key1': True}
>>>
Sharad
  • 7,257
  • 3
  • 18
  • 32