2

Possible Duplicate:
What are the u’s when I use json.loads?

Attempting to encode some string data into json in PHP via json_encode and pass it to my python script on the same server:

This data's going to contain quotes, and will have an embedded array full of strings of urls.

$data =array("test.com", array("http://google.com", "http://cnn.com"), "ASDASDASD");
$jsonified = json_encode($data);
shell_exec("python /path_to_script/script.py '".$jsonified."'");

which will hopefully run the script with the data as it's first argument.

Right now, I'm kinda rolling with just making sure it can load into a thing properly:

import json
import sys

encoded = sys.argv[1]
print "ENCODED: ", encoded

decoded = json.loads(encoded)
print "DECODED: ", decoded

and I'm calling it this way:

~$ python json-test.py '["test.com", ["http://www.google.com", "http://cnn.com"], "ASDASDASDASD"]'
ENCODED: ["test.com",["http://www.google.com", "http://cnn.com"], "ASDASDASDASD"]
DECODED: [u'test.com', [u'http://www.google.com', u'http://cnn.com'], u'ASDASDASDASD']

What's with the 'u's? Also am I correct in hoping this will somehow go together smoothly?

Community
  • 1
  • 1
Fal-Cone
  • 345
  • 1
  • 4
  • 16
  • 3
    the `u` simply means those are unicode strings – stranac Nov 27 '12 at 20:41
  • So I'd have to, like decoded.encode('utf-8') or something? – Fal-Cone Nov 27 '12 at 20:45
  • 1
    Are you sure you have no better way of passing data than as an argument on the command line? I am skeptical of utf-8 surviving through a trip to the shell and back. What about piping your input through stdout/stdin, a named pipe, a file, or communicating through a database, to name just a few options here. – Francis Avila Nov 27 '12 at 20:47
  • 1
    @Fal-Cone, most likely you don't have to do anything, but it depends on what you plan to do with the string. Unicode is what you want--it only seems strange to you because you're used to PHP. – Francis Avila Nov 27 '12 at 20:49
  • @Fal-Cone - that depends on what you need to do with the strings. Pleas read [this](http://docs.python.org/2/howto/unicode.html) before blindly trying to decode/encode strings. – mata Nov 27 '12 at 20:49
  • Just trying to turn them into python strings. – Fal-Cone Nov 27 '12 at 20:50
  • @Fal-Cone a "string of bytes" (e.g. utf-8) is not the same as "a string of unicode characters". You have a python unicode string, decoded from utf-8. You had better have a good reason to encode it again, like writing it to a file or something. – Francis Avila Nov 27 '12 at 20:52
  • I'm not being a stickler for encoding, I just want the data I'm passing through the command line to come out as normal python strings/arrays of strings intact and able to be used for python-stuff, and don't really have much of a willingness to stash the data in a file or in a database somewhere else when I can just pipe it directly to the script via command line. (I mean, I could pass the strings as individual arguments, but the possibility of an array terrifies me) – Fal-Cone Nov 27 '12 at 20:54
  • 1
    What makes "u" funky? I say "漢" has more funkfactor :) – Michel Feldheim Nov 27 '12 at 20:55

0 Answers0