Search code examples
pythonjsonpprint

JSON output has weird characters in JSON output pprint Python


In my rest calls (using YT API v3) I am getting strange 'u' characters in the JSON output.

I am using the pprint in python.

My code is a normal rest call:

import os

from io import StringIO
import json
import pprint

import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors


scopes = ["https://www.googleapis.com/auth/youtube.readonly"]

def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "xxxxxx.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.playlists().list(
        part="snippet",
        channelId="UC_ANPr8IkWibKlKhmi_-H1g"
    )
    response = request.execute()
    pprint.pprint(response)

    print(response)

if __name__ == "__main__":
    main()

My JSON output looks like this:

{u'etag': u'"p4VTdlkQv3HQeTEaXgvLePAydmU/hwD3N6ajbzX2GqxCVs32nBgZbs8"',
 u'items': [{u'etag': u'"p4VTdlkQv3HQeTEaXgvLePAydmU/9F4RmINu4drT-fTZjviHFXj3Yak"',
             u'id': u'PLejO9z7yhQOxjONeDVWaAy3kX3tEcImCR',
             u'kind': u'youtube#playlist',
             u'snippet': {u'channelId': u'UC_ANPr8IkWibKlKhmi_-H1g',
                          u'channelTitle': u'haramaininfo',
                          u'description': u'',
                          u'localized': {u'description': u'',
                                         u'title': u'Eid Takbeerat 1440'}, ....

See the u'xxxx': {u' .... ??

WHat is this and how do I get the correct JSON formatted out put??


Solution

  • Thanks everyone.

    I added import ast to my code and then used these code additions:

    jdata = ast.literal_eval(json.dumps(response))
    pprint.pprint(jdata)
    

    It is now printing correctly for me.