Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: make duplicate JSON keys an error

by vsespb (Hermit)
on Oct 24, 2013 at 17:22 UTC ( #1059521=note: print w/ replies, xml ) Need Help??


in reply to make duplicate JSON keys an error

You could try to write code (perhaps, a single regexp) which will prepend prefix to all keys in JSON string

{ "1_op": "add", "2_path": "/baz", "3_value": "qux", "4_op": "remove" + }

You don't even have to check if this string is a key (it's ok to damage values)
next, parse with JSON module, and when you find "1_op" and "4_op" in same hash - that would mean error.
UPD:
Alpha version:
use strict; use warnings; use JSON::XS; use Data::Dumper; my $s = <<"END"; [ { "op": "add", "path": "/baz", "value": "qux", "op": "remove" } ] END my $x = 1; $s =~ s/\"([^\"]+)\"/"\"".++$x."_".$1."\""/ge; my $j = JSON::XS->new()->filter_json_object(sub { my %seen; for (keys %{shift()}) { die unless /^\d+\_(.*)$/; die "key [$1] already seen" if $seen{$1}++; } }); $j->decode($s);
prints
key [op] already seen
UPD2:
You can even avoid double-parsing, just remove prepended numbers in filter_json_object and return correct data. In the end yo'll get correct hash on first pass.


Comment on Re: make duplicate JSON keys an error
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1059521]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2015-07-04 00:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls