problems decoding json objects
Hi people! My JSON String: from json.decoder import JSONDecoder myjs = '{AVName:Tamer,ANName:Higazi,AAnschrift:Bauerngasse,AHausnr:1,APLZ:55116,AOrt:Mainz},{KontaktTel:[01234,11223344],{ZahlungsArt:0},{ZugangsDaten:[tamer.hig...@nomail.com,mypass]}' If I try to decode it, with: JSD = JSONDecoder() rsx = JSD.decode(myjs) I get this error message: JSD.decode(myjs) Traceback (most recent call last): File stdin, line 1, in module File /usr/lib64/python2.7/json/decoder.py, line 368, in decode raise ValueError(errmsg(Extra data, s, end, len(s))) ValueError: Extra data: line 1 column 108 - line 1 column 220 (char 107 - 219) How do I solve this problem ?! For any help, thanks Tamer -- https://mail.python.org/mailman/listinfo/python-list
Re: problems decoding json objects
Tamer Higazi th9...@googlemail.com writes: myjs = '{AVName:Tamer,ANName:Higazi,AAnschrift:Bauerngasse,AHausnr:1,APLZ:55116,AOrt:Mainz},{KontaktTel:[01234,11223344],{ZahlungsArt:0},{ZugangsDaten:[tamer.hig...@nomail.com,mypass]}' That's not a valid JSON document. See URL:http://json.org/. You appear to have three documents in a row, separated by commas. Or one malformed document, missing its enclosure. Or something else; that's the trouble with a malformed document, we don't know what it should be. I get this error message: JSD.decode(myjs) Traceback (most recent call last): File stdin, line 1, in module File /usr/lib64/python2.7/json/decoder.py, line 368, in decode raise ValueError(errmsg(Extra data, s, end, len(s))) ValueError: Extra data: line 1 column 108 - line 1 column 220 (char 107 - 219) Right. The document ends there, and the rest of the string is trailing garbage, which is an error. How do I solve this problem ?! Fix the document. Where did it come from? Best, don't put it directly in the Python source code as a string. Ideally, have it as a distinct file, which you can examine in a text editor with JSON highlighting; and run separately through a JSON parser for correctness. -- \ “Isn't it enough to see that a garden is beautiful without | `\ having to believe that there are fairies at the bottom of it | _o__) too?” —Douglas Adams | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: problems decoding json objects
On Wed, May 14, 2014 at 3:36 AM, Tamer Higazi th9...@googlemail.com wrote: myjs = '{AVName:Tamer,ANName:Higazi,AAnschrift:Bauerngasse,AHausnr:1,APLZ:55116,AOrt:Mainz},{KontaktTel:[01234,11223344],{ZahlungsArt:0},{ZugangsDaten:[tamer.hig...@nomail.com,mypass]}' Following up on Ben's comment, what did you expect? With my Python hat on, I'd say you've got a string representing a tuple of four dictionaries, though my human brain can see past the missing '}' which is supposed to close the second dict. If you add that, it's still not valid JSON (I guess JSON doesn't have tuples). This stuff isn't meant to be typed by humans. Instead computers are supposed to barf it out and munch on it. As Ben indicated, putting it in a string is pointless. Might as well just define the dictionaries yourself. not-gonna-mention-eval-ly, y'rs Skip -- https://mail.python.org/mailman/listinfo/python-list
Re: problems decoding json objects
Ben Finney b...@benfinney.id.au writes: Tamer Higazi th9...@googlemail.com writes: How do I solve this problem ?! Fix the document. Where did it come from? If you're the one typing raw JSON into your program: Fix that. Don't type raw JSON. JSON is an object serialisation format; it's foolish to type it yourself. You should only be generating it from a known-debugged JSON library, or reading it as input from some other generator. If you want to define an object in a Python literal, type Python code. Skip Montanaro s...@pobox.com writes: This stuff isn't meant to be typed by humans. Instead computers are supposed to barf it out and munch on it. As Ben indicated, putting it in a string is pointless. It didn't even occur to me that the programmer would have typed this in manually. Good catch, Skip. -- \ “Why doesn't Python warn that it's not 100% perfect? Are people | `\ just supposed to “know” this, magically?” —Mitya Sirenef, | _o__) comp.lang.python, 2012-12-27 | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: problems decoding json objects
In article mailman.9998.1400063958.18130.python-l...@python.org, Skip Montanaro s...@pobox.com wrote: I guess JSON doesn't have tuples JSON has sequences, which both Python lists and tuples serialize as (losing the distinction between them). In the other direction, JSON sequences typically unpack as lists, but I think that's just convention. There might be json packages which unpack them as tuples. -- https://mail.python.org/mailman/listinfo/python-list
Re: problems decoding json objects
On Wed, 14 May 2014 10:36:11 +0200, Tamer Higazi wrote: Hi people! My JSON String: from json.decoder import JSONDecoder myjs = '{AVName:Tamer,ANName:Higazi,AAnschrift:Bauerngasse,AHausnr:1,APLZ:55116,AOrt:Mainz}, {KontaktTel:[01234,11223344],{ZahlungsArt:0},{ZugangsDaten: [tamer.hig...@nomail.com,mypass]}' If I try to decode it, with: JSD = JSONDecoder() rsx = JSD.decode(myjs) I get this error message: JSD.decode(myjs) Traceback (most recent call last): File stdin, line 1, in module File /usr/lib64/python2.7/json/decoder.py, line 368, in decode raise ValueError(errmsg(Extra data, s, end, len(s))) ValueError: Extra data: line 1 column 108 - line 1 column 220 (char 107 - 219) How do I solve this problem ?! For any help, thanks Try doing a manual inspection of your json string. It appears to contain several objects, but they're not contained within an outer object or an array. If you want a list of data objects, you need to put [] round them. If you want them to be dictionary elements, you need to wrap them with {} and put identifiers in. If you want a single dictionary, you need to remove most of the { and }. In addition, one of the objects doesn't appear to be terminated. {AVName:Tamer,ANName:Higazi,AAnschrift:Bauerngasse,AHausnr:1,APLZ:55116,AOrt:Mainz}, {KontaktTel:[01234,11223344] ** missing '}' ??? ** , {ZahlungsArt:0}, {ZugangsDaten:[tamer.hig...@nomail.com,mypass]} Depending how I correct the json string, I can create different values for rsx: (1) a list of 4 dictionaries: $ ./jsonerr.py [{u'APLZ': u'55116', u'ANName': u'Higazi', u'AHausnr': u'1', u'AVName': u'Tamer', u'AAnschrift': u'Bauerngasse', u'AOrt': u'Mainz'}, {u'KontaktTel': [u'01234', u'11223344']}, {u'ZahlungsArt': u'0'}, {u'ZugangsDaten': [u'tamer.hig...@nomail.com', u'mypass']}] (2) a single dictionary: $ ./jsonerr.py {u'APLZ': u'55116', u'KontaktTel': [u'01234', u'11223344'], u'ZugangsDaten': [u'tamer.hig...@nomail.com', u'mypass'], u'ANName': u'Higazi', u'AHausnr': u'1', u'AVName': u'Tamer', u'AAnschrift': u'Bauerngasse', u'AOrt': u'Mainz', u'ZahlungsArt': u'0'} (3) an object with 4 subsidiary objects: $ ./jsonerr.py {u'Misc data': {u'ZahlungsArt': u'0'}, u'Name and Addr': {u'APLZ': u'55116', u'ANName': u'Higazi', u'AHausnr': u'1', u'AVName': u'Tamer', u'AAnschrift': u'Bauerngasse', u'AOrt': u'Mainz'}, u'Login info': {u'ZugangsDaten': [u'tamer.hig...@nomail.com', u'mypass']}, u'Telephones': {u'KontaktTel': [u'01234', u'11223344']}} but I have no way of knowing which it is you require. -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: problems decoding json objects
You are right! It was all alone my mistake which I recently figured out. Thanks for helping me. Tamer Am 14.05.2014 11:20, schrieb Ben Finney: Tamer Higazi th9...@googlemail.com writes: That's not a valid JSON document. See URL:http://json.org/. -- https://mail.python.org/mailman/listinfo/python-list