Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #46427
| 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 | <malaclypse2@gmail.com> |
| 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 | <CAMZYqRT0F6i72Wozw8q3vfgi4xo7-ZtUEMJ6Q=9=ypRGvH_vQw@mail.gmail.com> |
| References | <CAOuJsMn38V2Gw3s=KMLJ+t4uFyu3GdzYyA-_TNiEqn7hWxNEjA@mail.gmail.com> <CAMZYqRT0F6i72Wozw8q3vfgi4xo7-ZtUEMJ6Q=9=ypRGvH_vQw@mail.gmail.com> |
| Date | Wed, 29 May 2013 23:43:18 -0400 |
| Subject | Re: detect key conflict in a JSON file |
| From | Jerry Hill <malaclypse2@gmail.com> |
| To | Chris Rebert <clp2@rebertia.com> |
| Content-Type | multipart/alternative; boundary=047d7b2e0a5792e2b904dde74cfd |
| Cc | Python mailing list <python-list@python.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2391.1369885409.3114.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
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#repeated-names-within-an-object
> http://docs.python.org/2/library/json.html#json.load
>
That 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
>>> s = '{"x": 1, "x": 2, "x": 3}'
>>> def json_checker(seq):
d = {}
for key, value in seq:
if key in d:
raise ValueError("Duplicate key %r in json document" % key)
else:
d[key]=value
return d
>>> json.loads(s, object_pairs_hook=json_checker)
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
json.loads(s, object_pairs_hook=checker)
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 = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python32\lib\json\decoder.py", line 367, in raw_decode
obj, end = self.scan_once(s, idx)
File "<pyshell#30>", line 5, in json_checker
raise ValueError("Duplicate key %r in json document" % key)
ValueError: Duplicate key 'x' in json document
--
Jerry
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: detect key conflict in a JSON file Jerry Hill <malaclypse2@gmail.com> - 2013-05-29 23:43 -0400
csiph-web