Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #71548

Re: problems decoding json objects

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.python
Subject Re: problems decoding json objects
Date 2014-05-14 12:04 +0000
Organization A noiseless patient Spider
Message-ID <lkvm3s$2sb$1@dont-email.me> (permalink)
References <mailman.9992.1400057914.18130.python-list@python.org>

Show all headers | View raw


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@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@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@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@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@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@gmail.com

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

problems decoding json objects Tamer Higazi <th982a@googlemail.com> - 2014-05-14 10:36 +0200
  Re: problems decoding json objects Denis McMahon <denismfmcmahon@gmail.com> - 2014-05-14 12:04 +0000

csiph-web