1

I'm using the following procedure to grab a json file and break it down into a dictionary file which I can further use. The code I'm using is as follows:

import urllib
import json

pricelist_link="http://foo.com/test.json"

price_instance=urllib.urlopen(pricelist_link)
print type(price_instance)

parsed_price_data = json.loads(price_instance.read())
print type(parsed_price_data)
print parsed_price_data

This appears to be the standard way of doing this (I've seen suggestions of using urllib2 instead, however that did not yield different results for me.

When I view the JSON file in the browser, it appears normal, with double quotes:

{"AK-47 | Aquamarine Revenge (Battle-Scarred)":{"2016-01-26":{"price":821,"count":19},"2016-01-27":{"price":762,"count":24},"2016-01-28":{"price":745,"count":22},"2016-01-29":...

However when I print the "parsed_price_data", it appears with a u before single quotes.

{u'P250 | Muertos (Battle-Scarred)': {u'2016-03-13': {u'count': 3, u'price': 118}, u'2016-03-17': {u'count': 3, u'price': 129}, u'2016-03-01': {u'count': 

I'm not worried about the double quotes going to single quotes, but what is with the u? Is there an easy way to remedy this without a cumbersome regex?

Bennett Talpers
  • 802
  • 6
  • 9
  • 2
    What you're seeing is the python object representation of the same data. If you use `json.dumps` to turn it back into a json string, it'll print out the same as your browser. – tzaman Mar 26 '16 at 08:09

3 Answers3

2

String1 = 'abcd' String 2 = u'abcd'

Don't panic. 'u' just indicates Unicode, json dumps string values as Unicode type . You probably won't need to fix it, for in python if you try to compare 'string1' with 'string2' in the above example it returns True.

I'm not sure about this, but I think in python3+ all strings are Unicode by default, and 'u' prefix is used for older versions. So my guess is you're using python 2.7 .

akash12300
  • 412
  • 2
  • 12
0

The prefix u is simply an indication of the type of your object, which is unicode instead of str (see this).

If you wanted to see your parsed_price_data in the same way as in your browser, you could simply do:

json.dumps(parsed_price_data)
DevShark
  • 8,558
  • 9
  • 32
  • 56
0

u'the string' is the unicode representation in python code, as well as the console output (as you see from the print result).

Your json source file should be encoded by utf-8, as the default encoding used by json.load is utf-8.

Lei Shi
  • 757
  • 4
  • 8