fork(7) download
  1. def nested_pairs2dict(pairs):
  2. d = {}
  3. for k, v in pairs:
  4. if isinstance(v, list): # assumes v is also list of pairs
  5. v = nested_pairs2dict(v)
  6. d[k] = v
  7. return d
  8.  
  9. import ast
  10. import json
  11. import sys
  12.  
  13. print(json.dumps(nested_pairs2dict(ast.literal_eval(sys.stdin.read())),
  14. sort_keys=True, indent=4))
Success #stdin #stdout 0.05s 9872KB
stdin
[
  ['key1', 'value1'],
  ['key2', 
     [
       ['key3', 'value3'],
       ['key4', 
          [
 
          ]
       ],
       ['key5', 'value5']
     ]
  ],
  ['key6', 'value6'],
]
stdout
{
    "key1": "value1", 
    "key2": {
        "key3": "value3", 
        "key4": {}, 
        "key5": "value5"
    }, 
    "key6": "value6"
}