I'm trying to insert a unix timestamp using REST to a webservice. And when I convert the dictionary I get the value: 1392249600000L I need this value to be an integer.
So I tried int(1392249600000L) and I get 1392249600000L, still a long value.
The reason I need this is because the JSON webservice only accepts timestamsp with milliseconds in them, but when I pass the JSON value with the 'L' in it I get an invalid JSON Primative of value 1392249600000L error.
Can someone please help me resolve this? It seems like it should be so easy, but it's driving me crazy!
You should not be using Python representations when you are sending JSON data. Use the json module to represent integers instead:
>>> import json
>>> json.dumps(1392249600000L)
'1392249600000'
In any case, the L is only part of the string representation to make debugging easier, making it clear you have a long, not int value. Don't use Python string representations for network communications, in any case.
For example, if you have a list of Python values, the str() representation of that list will also use repr() representations of the contents of the list, resulting in L postfixes for long integers. But json.dumps() handles such cases properly too, and handle other types correctly too (like Python None to JSON null, Python True to JSON true, etc.):
>>> json.dumps([1392249600000L, True, None])
'[1392249600000, true, null]'
Related
This is a simple questions that is really only a footnote in something I am writing:
Is any valid JSON not also valid Python?
I know the converse is true, i.e. Python data structures and scalars allow a variety of constructs that are not JSON. But for the most part, JSON seems to be a subset of Python syntax for defining (some) data structures.
The obvious stuff is covered. Strings are strings. Ints are ints. JSON "numbers" are read as Python floats (although RFC 8259 does not mandate that interpretation vs. fixed point, for example). Dicts are dicts. Lists are lists.
But maybe something in some obscure corner violates the subset relationship. For example, is there anything in the encoding of Unicode outside the BMP that is directly incompatible? Or maybe within Unicode surrogate pairs?
Or maybe something with numbers where some large number of digits after the decimal would be technically valid JSON but not Python? (I don't think so, but just trying to think of scenarios).
The most obvious thing is that true, false and null don't exist in Python. They are called True, False and None.
In addition, \/ in strings is interpreted as / in json and as \/ in Python:
>>> a = '"\/"'
>>> print(a)
"\/"
>>> print(eval(a))
\/
>>> print(json.loads(a))
/
Yes, you are correct, every valid JSON can be handled in Python. Python is a complete language, and JSON is a way of storing data (serialisation maybe?). Generally a language will support everything a JSON object can represent.
There would be different representation for sure like true in JSON is True in Python.
Since, JSON is way of storing data, and we can also pass it around HTTP requests, which are always processed by some server side language, which is expected to handle the JSON object.
I have a long string that almost looks like a dictionary. I want to convert this to a proper Python dictionary. An example of the string is below:
'{"autorunResult":"0","batteryInfo":"No system battery","cpuBrand":"Intel(R) Xeon(R) CPU E5-1650 v3 # 3.50GHz","id":"bMlXyTrjXOOo","localeId":"1033","numCores":"1","payloadResult":"0","processorArchitecture":"x64 (AMD or Intel)","systemMemory":"0.2 GB","v":"5","windowsVersion":"Windows 7 Service Pack 1","payloadSaved":true,"autorunSaved":true,"installedApps":["AddressBook","Adobe AIR","com.adobe.mauby.4875E02D9FB21EE389F73B8D1702B320485DF8CE.1","Connection Manager","DirectDrawEx","Fontcore","IE40","IE4Data","IE5BAKEX","IEData","MobileOptionPack","Pillow-py2.7","SchedulingAgent","WIC","{00203668-8170-44A0-BE44-B632FA4D780F}","{26A24AE4-039D-4CA4-87B4-2F83217000FF}","{32A3A4F4-B792-11D6-A78A-00B0D0170000}","{4A03706F-666A-4037-7777-5F2748764D10}","{77DCDCE3-2DED-62F3-8154-05E745472D07}","{AC76BA86-7AD7-1033-7B44-A90000000001}","{BB8B979E-E336-47E7-96BC-1031C1B94561}","{C3CC4DF5-39A5-4027-B136-2B3E1F5AB6E2}"],"autoRunApps":["OptionalComponents","Adobe Reader Speed Launcher","SunJavaUpdateSched","MFDS"]}'
Note that this looks like a string representation of a dictionary. In fact, it is not. These two k,v pairs kill it: "payloadSaved":true,"autorunSaved":true. (no double-quotes around the values).
Basically, I need to take the long input string and convert it to a dictionary. Any tricks?
I tried:
using ast.literal_eval. It bombs...because of the above issue. Need to somehow sanitize the input string so that ast works.
Take out the parenthesis, tokenize the long string on comma, but again, it bombs...(the list values have commas...).
Not sure how to proceed.
If that is JSON, then:
import json
d = json.loads(s)
If that is Python file:
d = eval(s)
For the string keys & values you will find no much difference. The difference may appear when true/True or false/False or null/None values appear, or on how the lists/dicts are serialized in some cases.
I have a very large number of database objects from SQLite3 to loop over and to print to terminal. I'm trying to have a robust method that can be applied to every object retreived from the database such that it is converted to a string. The objects are likely to be strings, ints, floats and unicode.
My initial approach was to simply use the function str() on every object, but this fails on some unicode. I was prompted then to try to use .encode("utf-8") on every object, but this fails on ints ('int' object has no attribute 'encode'). What would be a compact way to try to convert these objects to strings?
The best I've got right now is something like the following:
try:
string_representation = str(row[column])
except:
string_representation = str(row[column].encode("utf-8"))
row_contents.append(string_representation)
Is there a better, perhaps more compact approach? A one-liner would be nice.
Call unicode() on the numeric values.
But if you have a collection that includes both unicode string and byte strings, and you want to avoid any implicit encoding, you would have to check the types.
In C you would call
const unsigned char sqlite3_column_text(sqlite3_stmt, int iCol);
which will return whatever converted to text
https://www.sqlite.org/c3ref/column_blob.html
Dunno what you would do in python - it is better to use the database engine for these things = faster and more reliable.
I have, an an input, integers. I build a dict out of them (as values). I then dump it to JSON, to be reused elsewhere.
I ultimately (at the "elsewhere") need to use these integers as strings. I can therefore do the conversion
upstream (when building the dict)
or on the receiving side (the "elsewhere").
There will be, no matter the solution, plenty of str() all over the code.
I am looking for a cleaner way to convert all the int into str in that specific case.
One idea is to recursively parse the dict once it is built, just before the json.dumps(), and make the conversion there.
I am wondering, though, if there is not a way to handle this while dumping the JSON? Possibly with json.JSONEncoder? (which to be frank I do not understand much)
Assuming that on the receiving end you are running Python, you could convert the ints to strings upon loading the JSON by using the parse_int parameter:
import json
test = {'foo':1, 'bar':[2,3]}
json_str = json.dumps(test)
print(json.loads(json_str, parse_int=str))
yields
{u'foo': '1', u'bar': ['2', '3']}
Per the docs:
parse_int, if specified, will be called with the string
of every JSON int to be decoded. By default this is equivalent to
int(num_str). This can be used to use another datatype or parser
for JSON integers (e.g. float).
Let's say I want to create a json object following the structure:
{"favorite_food":["icecream","hamburguers"]}
to do so in python, if i know the whole string in advance, I can just do:
json.dumps({"favorite_food":["icecream","hamburguers"]})
which works fine.
my question though is, how would i do the same thing if i wanted to get the object as a result of a string interpolation? For example:
favorite food = 'pizza'
json.dumps({"favorite_food":[%s]}) %favorite_food
the issue i found is, if I do the interpolation prior to calling the json.dumps:
dict= '{"favorite_food":[%s]}' % favorite_food
if i then do json.dumps(dict) , because of the string quotation, the json_dumps returns:
{"favorite_food":[pizza]}
that is, is not a dict anymore (but a string with the structure of a dict)
How can i solve this simple issue?
Why not just:
>>> food = "pizza"
>>> json.dumps({"favorite_food":[food]})
'{"favorite_food": ["pizza"]}'
json,dumps takes actual values as input --- that is, real dicts, lists, ints, and strings. If you want to put your string value in the dict, just put it in. You don't want to put in a string representation of it, you want to put in the actual value and let json.dumps make the string representation.
How about below:
favorite_food = 'pizza'
my_dict = {"favorite_food":[favorite_food]}
print json.dumps(my_dict)
I found this is very simple.