9

I have trouble parsing a json object with perl and I don't know if the issue is coming from the json or my script. Here is the json:

test.json

{
   "count":3,
   "entries": 
   [
      {
         "id":85,
         "application":AuditExampleLogin1,
         "user":admin,
         "time":"2011-11-22T10:29:37.422Z",
         "values":
null
      },
      {
         "id":87,
         "application":AuditExampleLogin1,
         "user":admin,
         "time":"2011-11-22T10:30:56.235Z",
         "values":
null
      },
      {
         "id":89,
         "application":AuditExampleLogin1,
         "user":admin,
         "time":"2011-11-22T10:33:15.000Z",
         "values":
null
      }
   ]
}

Here the script:
script.pl

#!/usr/bin/perl -w
use strict;
use JSON;
open FILE, 'test.json' or die "Could not open file inputfile: $!";
sysread(FILE, my $result, -s FILE);
close FILE or die "Could not close file: $!"; 
my @json = @{decode_json($result)};

And finally the error I'm getting:
error

malformed JSON string, neither array, object, number, string or atom,
at character offset 86 (before "AuditExampleLogin1,\n...") at     
./script.pl line 7.

Q: Can someone tell me whether the issue is coming from the json or my script and what to change in either case?

FYI the json is coming from Alfresco auditing api.

2
  • 3
    AFAIK that's not valid JSON, missing quotes around the values. Commented Nov 22, 2011 at 14:46
  • I was dealing with the exactly same problem. I have a question to the thread author: how do you access the @json variables? I keep geting that @json is a HASH in the memory but I can`t access the variables or loop trough it. Commented Jun 14, 2012 at 16:11

2 Answers 2

12

This:

"application":AuditExampleLogin1,

… is invalid JSON. AuditExampleLogin1 isn't an array, object, number, string or atom. I don't know what it is supposed to be, so I can't tell you what to change it to.

If it is a string, then you need to enclose it in ".

See also: JSON Lint.

Sign up to request clarification or add additional context in comments.

Comments

3

It works if the Audit... and admin values are quoted. The line

my @json = @{decode_json($result)};

needs to be just

my @json = decode_json($result);

3 Comments

indeed it works when application and user values are quoted. Is there a way to tell perl to be a bit more loose on the parsing of JSON and accept unquoted strings? (that would save me from having to rewrite the json).
Not sure, the JSON module author considers strict checking to be a security feature. You might poke at the code, JSON::PP is a pure perl version.
You're asking a JSON parser to successfully parse data that isn't valid JSON. That way lies madness, surely :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.