python - convert encoded json into utf-8
Question: I have several json files that need to be handled in a python script, although
it seems to NOT to be in a valid json format:

    
    
    {
      'data': [
    
        {
         'ad_id': u'6038487',
         'adset_id': u'6038483800',
         'campaign_id': u'603763200',
         'created_time': u'2015-12-17T15:26:04+0000',
         'field_data': [
              {u'values': [u'Fahrrad'], u'name': u'what is your vehicle?'},
              {u'values': [u'Coco'], u'name': u'first_name'},
              {u'values': [u'Homer'], u'name': u'last_name'},
              {u'values': [u'aaa@hotmail.de'], u'name': u'email'},
              {u'values': [u'+490999999'], u'name': u'phone_number'}
              ], 'id': u'5655545710'
        },
        {
       'ad_id': u'39392400',
       'adset_id': u'39366200',
       'campaign_id': u'39363200',
       'created_time': u'2014-12-16T13:01:52+0000',
       'field_data': [
               {u'values': [u'Frankfurt'], u'name': u'in_welcher_stadt_m\xf6chtest_du_arbeiten?'},
               {u'values': [u'Auto'], u'name': u'what is your vehicle?'},
               {u'values': [u'Homer'], u'name': u'first_name'},
               {u'values': [u'abc'], u'name': u'last_name'},
               {u'values': [u'XYZ@gmail.com'], u'name': u'email'},
               {u'values': [u'0555555555'], u'name': u'phone_number'}
                ],
        'id': u'149809770'
        }
        ]
    }
    

  1. it has single-quotes instead of double-quotes 
  2. is encoded (seethe `u`) 
  3. some letters are encoded e.g. `\xf6` that represents `ö`

ideally, the json should be possible to read with the snippet:

    
    
    import json
    import pprint
    
    
    with open('leads.json') as data_file:
        data = json.load(data_file)
    
    pprint(data)
    

**How can I convert the input json into a valid json in utf-8 format?**


Answer: As I said, that's not JSON, it's a printed representation of a Python object
(which happens to look similar to JSON). To safely import it, you can use
`ast.literal_eval`:

    
    
    from pprint import pprint
    import ast
    with open('leads.json') as data_file:
           data = ast.literal_eval(data_file.read())
    pprint(data)
    

