-1

I want to remove the u (unicode value) from the results. Other tools is not reading it in JSON format.

import requests
import json

headers={
        "accept": "application/json",
        "content-type": "application/json"
    }

test_urls = ['https://google.com']


def return_json(url):
    try:
        response = requests.get(url,headers=headers)

        # Consider any status other than 2xx an error
        if not response.status_code // 100 == 2:
            return "Error: Unexpected response {}".format(response)

        json_obj = response.json()
        return json_obj
    except requests.exceptions.RequestException as e:
        # A serious problem happened, like an SSLError or InvalidURL
        return "Error: {}".format(e)


for url in test_urls:

    print "Fetching URL '{}'".format(url)
    print return_json(url)

Results:

{u'rows': [{u'timestamp': 1585924500001L, u'RAM_': 1000, u'Allocated': 4000.78, u'queue': 0, u'Details':  u'Connected': 2, u'Queue': 0, u'EventsQueue': 0}]

I want results without u in the value

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 4
    See https://stackoverflow.com/q/2464959/3001761 and stop using Python 2 – jonrsharpe Apr 03 '20 at 14:51
  • Unicode strings are the *right* kind of string (when what you want is in fact a "string", and not a byte sequence serialized to go over the wire / into a file / etc). This is a feature, not a bug; you should not try to change it. Just switch to Python 3 where these strings are printed without the `u` in front (and the other type -- bytestrings -- are instead printed with a `b` in front). – Charles Duffy Apr 03 '20 at 15:24
  • ...which is to say, the `u`s are not "in the values" at all. They're just sigils describing the datatype, and the data type is the best one for the purpose. – Charles Duffy Apr 03 '20 at 15:29
  • In reviewing answers on the linked questions, keep in mind that you can access the raw text of a response with `response.text`, and thus pass it directly to whatever JSON decoding logic you want. – Charles Duffy Apr 03 '20 at 15:30

1 Answers1

0

You can use encode() method to convert unicode strings to ascii strings like this :

import requests
import json

headers={
        "accept": "application/json",
        "content-type": "application/json"
    }

test_urls = ['http://www.mocky.io/v2/5185415ba171ea3a00704eed']


def return_json(url):
    try:
        response = requests.get(url,headers=headers)

        # Consider any status other than 2xx an error
        if not response.status_code // 100 == 2:
            return "Error: Unexpected response {}".format(response)

        json_obj = response.json()
        return json_obj
    except requests.exceptions.RequestException as e:
        # A serious problem happened, like an SSLError or InvalidURL
        return "Error: {}".format(e)


for url in test_urls:

    print("Fetching URL '{0}'".format(url))
    ret = return_json(url)
    ret = {unicode(k).encode('ascii'): unicode(v).encode('ascii') for k, v in ret.iteritems()}
    print(ret)
alefmim
  • 121
  • 9
  • 1
    In [How to Answer](https://stackoverflow.com/help/how-to-answer), see the section *Answer Well-Asked Questions*, and therein the bullet point regarding questions which "have been asked and answered many times before" – Charles Duffy Apr 03 '20 at 15:26
  • Sorry, I'm new here. I'll keep that in mind. Thank you. – alefmim Apr 03 '20 at 18:11
  • @alefmim still the output is enclosed with U' unicode value. – premranjith Apr 06 '20 at 07:30