I'm processing some JSON files using JSON and getting this error:

malformed JSON string, neither tag, array, object, number, string or a +tom, at character offset 0 (before "\x{feff}[{"registered...")
So I printed out the JSON file from the Perl script and sure enough there are three rogue characters before the opening square bracket. These do not show up in my text editor TextPad.

A search has found this explanation. However, the JSON files are being pulled from a UK Government data source and I have no control over how they are made. So I have to deal with the character(s) somehow.

Here's my test code:

use strict; use warnings; use JSON; use Data::Dumper; $/ = undef; open my $fh, '<', 'charity.json'; my $data = <$fh>; close $fh; print unpack("W", substr($data, 0, 1)) . ' - ' . unpack("W", substr($data, 1, 2)) . ' - ' . unpack("W", substr($data, 2, 3)) . "\n\n"; $data =~ s/.*?\[/\[/; # <-- fudge to clear character(s) my $json = decode_json $data; print Dumper $json; <code> Using this JSON file as a test... <code> [{"registered":true,"insolvent":false,"administration":true,"test":fal +se}]

Unpacking the first three characters gives 239 - 187 - 191 and the substitution seems to have the desired effect but it seems to be a bit of a fudge!

Can you suggest a "better" way to deal with this?

The output from Data::Dumper is a bit strange:

$VAR1 = [ { 'administration' => bless( do{\(my $o = 1)}, 'JSON::PP::Bo +olean' ), 'registered' => $VAR1->[0]{'administration'}, 'test' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ), 'insolvent' => $VAR1->[0]{'test'} } ];
I've come across  bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ) before instead of false but not $VAR1->[0]{'test'}. I guess this is so Data::Dumper doesn't have to create an object for each boolean. It instead it represents them in terms of ones it has previously created. Is that about right?

I have proved that this is just Data::Dumper and not the underlying data structure by this dereference:

foreach my $key(keys %{@{$json}[0]}) { print "$key - "; print ${@{$json}[0]}{$key}; print "\n"; }
Which produces zeros and ones for false and true...


In reply to Rogue character(s) at start of JSON file by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.