1

I just want to ask something about json and python.

If I'm correct, loading a StringIO to json would result to it having a u'.

for example, in the json library:

>>> from StringIO import StringIO
>>> io = StringIO('["streaming API"]')
>>> json.load(io)

results to

[u'streaming API']

so how do I parse this data? in order for me to sort a data that looks like this? I have this data I want to parse.

{u'brix': {u'Nov 29, 2017 11:20:15 PM': {u'Checklist': {u'Coffee tray with 3 coffee sticks, 3 creamer, 3 white sugar, 3 brown sugar, 1 equal or sweetener, 3 lipton tea, 2 mineral water, 3 cocktail napkin ?': u'No', u'Luggage bench fabric top is clean': u'No', u'1 facial tissue in a tissue box': u'No', u'Towel Reminder': u'No', u'1 pringles, 1 cashew nut, 1 cup noodles (placed in the coffee tray on the writing desk)?': u'No',

After reading a bit about json I found out that this may be a StringIO instead of a json. It was silly that I tried to do json.loads with it when I it's already loaded. All I want is to sort all of these data so it appears properly on web.

How do i properly do this? Do i decode these to json first so it becomes something like

{
 "maps":[
         {"id":"blabla","iscategorical":"0"},
         {"id":"blabla","iscategorical":"0"}
        ],
"masks":
         {"id":"valore"},
"om_points":"value",
"parameters":
         {"id":"valore"}
} 

Then get a value from that using this?

data["maps"][0]["id"]  # will return 'blabla'
data["masks"]["id"]    # will return 'valore'
data["om_points"]      # will return 'value'

Anyway, I'm confused of what I should do, I tried pretty much everything, these example came from other questions .

Here's a bit of my code:

result1 = firebase.get('/Rooms/Room1/2017-11-29/Inspection/Scan-in/Inspector/', None)
result2 = firebase.get('/Rooms/Room1/2017-11-29/Inspection/Scan-out/Inspector/', None)
print result1["brix"]

Firebase is supposed to return a json file. lol but it doesn't work on mine.

Thankyou very much to those who will clear it out to me.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Momochan
  • 103
  • 1
  • 8
  • 1
    seems you are receiving Unicode json, try converting it to normal json for parsing via loads function.. else you can try using dumps method. [refer for more details](https://stackoverflow.com/questions/36954511/convert-unicode-json-to-normal-json-in-python) Possible Solution in : https://stackoverflow.com/questions/13940272/python-json-loads-returns-items-prefixing-with-u – segFaulter Dec 03 '17 at 13:32

1 Answers1

0

you are receiving a Unicode json just as the comment says, if you want the output there's no need to convert to normal function. You can simply do something like this:

result1 = firebase.get('/Rooms/Room1/2017-11-29/Inspection/Scan-in/Inspector/', None)
result2 = firebase.get('/Rooms/Room1/2017-11-29/Inspection/Scan-out/Inspector/', None)
result1 = result1["brix"]["Nov 29, 2017 11:20:15 PM"]["Checklist"]["Luggage bench fabric top is clean"]

You will be receiving the

No

string, without the 'u

Here's the reference: https://docs.python.org/2/library/json.html

Look at the Decoding JSON part, there's the StringIO import, refer there for more information if you haven't read it yet.

Rekt
  • 359
  • 2
  • 18