-2

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??

ironmantis7x
  • 807
  • 2
  • 23
  • 58

3 Answers3

2

Those are unicode strings. The u'...' is simply a matter of representation, distinguishing them from ASCII strings.

If this representation bothers you, switch to Python 3 (which has native unicode strings) now. You have 8 days precisely.

Seb
  • 4,422
  • 14
  • 23
1

This u indicates that it is a Unicode string . You can access this response object same way as you access a dictionary/json.

Ex. reponse['etag']

Will give you correct value .

nishant
  • 2,526
  • 1
  • 13
  • 19
-1

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.

ironmantis7x
  • 807
  • 2
  • 23
  • 58