python - how to extract a list of lists (containing any value type) from json -


from mobile client, we're sending list of lists part of json server(python 2.7). needs split individual lists processing. example json = {"table":"[[1,2,3],[2,3,4],....]"}

what need extract individual lists [1,2,3],[2,4,5] etc, add object @ start of list , insert them postgres db.

for extracting json use :

post_data = json.loads(request.data) table = post_data['table']

print table prints u'[[1,2,3],[2,3,4]]'.

i checked couple of questions , use following snippet processing , inserting db.

tbl = post_data['table'] table = ast.literal_eval(tbl) print table  tup_table = [] lst in table :     lst.insert(0,userid)     tup_table += [tuple(lst)]  connection = engine.raw_connection() cur = connection.cursor() records_list_template = ','.join(['%s'] * len(tup_table)) insert_query = 'insert stats(userid,rid,val,fid) values {0}'.format(records_list_template) cur.execute(insert_query, tup_table) connection.commit() connection.close() 

the problem ast.literal_eval() works fine in example not work when list in json contains values of other types. e.g when list [[1,abc,3],[2,bcd,4]] returns error valueerror('malformed string')

i've tried using table.encode('utf-8') instead. returns list type loop loop not extract sub-list , extracts each character in list separately.

is there better way of extracting list unicode , fetching sublist creating tuple ?

like error message indicates, given json file malformed. forgot quotes.

change [[1,abc,3],[2,bcd,4]] [[1,"abc",3],[2,"bcd",4]].


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -