Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'win32': 0.03; 'duplicate': 0.07; 'json': 0.07; 'subject:file': 0.07; 'key.': 0.09; 'keys,': 0.09; 'raises': 0.09; 'valueerror:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'key)': 0.16; 'obj,': 0.16; 'subject:key': 0.16; 'url:json': 0.16; 'valueerror': 0.16; '\xc2\xa0if': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'bit': 0.19; 'first.': 0.19; '>>>': 0.22; 'example': 0.22; 'cc:addr:gmail.com': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**1': 0.23; '>>>': 0.24; '>': 0.26; 'skip:" 30': 0.26; 'second': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; '"",': 0.31; 'loads': 0.31; 'sep': 0.31; 'file': 0.32; 'url:python': 0.33; '(most': 0.33; 'skip:& 30': 0.33; 'received:209.85': 0.35; 'received:google.com': 0.35; "didn't": 0.36; 'url:org': 0.36; 'received:209': 0.37; 'growing': 0.38; 'skip:o 20': 0.38; 'skip:& 10': 0.38; 'url:library': 0.38; 'pm,': 0.38; 'recent': 0.39; 'easy': 0.60; 'manually': 0.60; 'skip:\xc2 10': 0.60; 'skip:o 30': 0.61; 'happen': 0.63; 'provide': 0.64; 'skip:\xe2 10': 0.65; 'repeat': 0.74; '\xc2\xa0\xc2\xa0': 0.74; 'examples.': 0.84; 'rebert': 0.84; 'silently': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=n4Mx98saabVHZery4ciajGPkEILl9ArgY3ok64p8Qkg=; b=XXChZGgz79aqLAVNBN+yAfJB6ejpN3HESCHGGNWtqm8HA7JnLuTRcQRWdyOTJ/4uHR Xovojt77WmAzj1uZvGizo1Rj7Nr0EEfcPIlaZUBWB6Cib9Mnmzo0W2neN2NHFg0j5lUe dMLGqwOytIeqCHhdiDw9UE2prOIVCToY7mlG3UStwhw0WRYXkld5J8jy6duMI5JjeW1k H3i9IBU6rZEFmwIxOlmZGfIDWDsQv3YbPF9OlN+7J0RCIuUfUZMSBh9iZ2xjQUugCGE2 7to6uMjRuqYnk39kaRF/MQ85jIJC1neJICW9f7+boTE20QWXeubO+Z/SIAcngRnUBnnm Sn8w== MIME-Version: 1.0 X-Received: by 10.68.252.227 with SMTP id zv3mr6037555pbc.14.1369885398719; Wed, 29 May 2013 20:43:18 -0700 (PDT) In-Reply-To: References: Date: Wed, 29 May 2013 23:43:18 -0400 Subject: Re: detect key conflict in a JSON file From: Jerry Hill To: Chris Rebert Content-Type: multipart/alternative; boundary=047d7b2e0a5792e2b904dde74cfd Cc: Python mailing list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 116 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369885409 news.xs4all.nl 15960 [2001:888:2000:d::a6]:34337 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46427 --047d7b2e0a5792e2b904dde74cfd Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Wed, May 29, 2013 at 1:10 PM, Chris Rebert wrote: > On Wed, May 29, 2013 at 4:16 AM, Jabba Laci wrote: > > I have a growing JSON file that I edit manually and it might happen > > that I repeat a key. If this happens, I would like to get notified. > > Currently the value of the second key silently overwrites the value of > > the first. > > You can pass an appropriate object_pairs_hook function to json.load(): > http://docs.python.org/2/library/json.html#repeated-names-within-an-objec= t > http://docs.python.org/2/library/json.html#json.load > =E2=80=8BThat makes it pretty easy to provide any validation you might like= to your JSON data. Here's a quick example that raises ValueError on duplicate keys, since the docs didn't have any examples. Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32 =E2=80=8B>>> s =3D '{"x": 1, "x": 2, "x": 3}' >>> def json_checker(seq): d =3D {} for key, value in seq: if key in d: raise ValueError("Duplicate key %r in json document" % key) else: d[key]=3Dvalue return d >>> json.loads(s, object_pairs_hook=3Djson_checker) Traceback (most recent call last): File "", line 1, in json.loads(s, object_pairs_hook=3Dchecker) File "C:\Python32\lib\json\__init__.py", line 320, in loads return cls(**kw).decode(s) File "C:\Python32\lib\json\decoder.py", line 351, in decode obj, end =3D self.raw_decode(s, idx=3D_w(s, 0).end()) File "C:\Python32\lib\json\decoder.py", line 367, in raw_decode obj, end =3D self.scan_once(s, idx) File "", line 5, in json_checker raise ValueError("Duplicate key %r in json document" % key) ValueError: Duplicate key 'x' in json document -- =E2=80=8B Jerry=E2=80=8B --047d7b2e0a5792e2b904dde74cfd Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

= On Wed, May 29, 2013 at 1:10 PM, Chris Rebert <clp2@rebertia.com> wrote:
On Wed,= May 29, 2013 at 4:16 AM, Jabba Laci <jabba.laci@gmail.com> wrote:
> I have a growing JSON file that I edit manually and it might happen > that I repeat a key. If this happens, I would like to get notified. > Currently the value of the second key silently overwrites the value of=
> the first.

You can pass an appropriate object_pairs_hook function to json.load()= :
http://docs.python.org/2/library/json.html#re= peated-names-within-an-object
http://docs.python.org/2/library/json.html#json.load

=E2=80=8BThat makes it pretty easy to provide any validation you might like= to your JSON data. Here's a quick example that raises ValueError on du= plicate keys, since the docs didn't have any examples.

Python 3.= 2.2 (default, Sep=C2=A0 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on wi= n32

=E2=80=8B>>> s =3D '{"x": 1, "x": 2, &= quot;x": 3}'
>>> def json_checker(seq):
=C2=A0=C2= =A0 =C2=A0d =3D {}
=C2=A0=C2=A0 =C2=A0for key, value in seq:
=C2=A0= =C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0if key in d:
=C2=A0=C2=A0 =C2=A0=C2=A0= =C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0raise ValueError("Duplicate key %r in = json document" % key)
=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0else:
=C2=A0=C2=A0 =C2=A0=C2=A0=C2= =A0 =C2=A0=C2=A0=C2=A0 =C2=A0d[key]=3Dvalue
=C2=A0=C2=A0 =C2=A0return d<= br>
>>> json.loads(s, object_pairs_hook=3Djson_checker)
Trac= eback (most recent call last):
=C2=A0 File "<pyshell#31>"= ;, line 1, in <module>
=C2=A0=C2=A0=C2=A0 json.loads(s, object_pairs_hook=3Dchecker)
=C2=A0 Fil= e "C:\Python32\lib\json\__init__.py", line 320, in loads
=C2= =A0=C2=A0=C2=A0 return cls(**kw).decode(s)
=C2=A0 File "C:\Python32= \lib\json\decoder.py", line 351, in decode
=C2=A0=C2=A0=C2=A0 obj, end =3D self.raw_decode(s, idx=3D_w(s, 0).end())=C2=A0 File "C:\Python32\lib\json\decoder.py", line 367, in raw_= decode
=C2=A0=C2=A0=C2=A0 obj, end =3D self.scan_once(s, idx)
=C2=A0 = File "<pyshell#30>", line 5, in json_checker
=C2=A0=C2=A0=C2=A0 raise ValueError("Duplicate key %r in json document= " % key)
ValueError: Duplicate key 'x' in json document
=
--
=E2=80=8B
Jerry=E2=80=8B

--047d7b2e0a5792e2b904dde74cfd--