problems decoding json objects

Denis McMahon denismfmcmahon at gmail.com
Wed May 14 08:04:12 EDT 2014


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.higazi at 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.higazi at 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.higazi at nomail.com', u'mypass']}]

(2) a single dictionary:

$ ./jsonerr.py
{u'APLZ': u'55116', u'KontaktTel': [u'01234', u'11223344'], 
u'ZugangsDaten': [u'tamer.higazi at 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.higazi at 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, denismfmcmahon at gmail.com



More information about the Python-list mailing list