Ankur_kuls has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I am running a perl script which uses JSON module. Below is script part which is doing the decoding

 my $data = decode_json $line;

Now while running this script I am getting the below error

File2: VSCDR_0001_00150_20150721-102225.json ':' expected, at character offset 193 (before "@serialNumber":"4000... +") at /root/reportscr/scripts/vs_state_change_dr_UW.pl line 103

Now as the error suggested line 193 of json file VSCDR_0001_00150_20150721-102225.json contains the error. Below are part of json file from line no. 191 to 195. (middle record is record no- 193).

{"@serialNumber":"235665321979", "@timestamp":"20150721T10:22:59+0530" +, "@subscriberId":"7409808075", "@transactionId":"394345856161456", " +@operatorId":"", "@voucherGroup":"B9", "@expiryDate":"20160731T23:59: +59+0530", "@state":"RESERVED", "@reserveTimeoutResolution":"PENDING", + "@subState":"", "@serviceType":"RESERVE_RECORD", "@value":"1554", "@ +agent":"", "@batchId":"10003"} {"@serialNumber":"235665321979", "@timestamp":"20150721T10:22:59+0530" +, "@subscriberId":"7409808075", "@transactionId":"394345856161456", " +@operatorId":"", "@voucherGroup":"B9", "@expiryDate":"20160731T23:59: +59+0530", "@state":"USED", "@reserveTimeoutResolution":"", "@subState +":"", "@serviceType":"COMMIT_RECORD", "@value":"1554", "@agent":"", " +@batchId":"10003"} {"@serialNumber":"400003262400", "@timestamp":"20150721T10:22:59+0530" +, "@subscriberId":"9675312531", "@transactionId":"442230856161952", " +@operatorId":"", "@voucherGroup":"A9", "@expiryDate":"20160817T23:59: +59+0530", "@state":"RESERVED", "@reserveTimeoutResolution":"PENDING", + "@subState":"", "@serviceType":"RESERVE_RECORD", "@value":"777", "@a +gent":"", "@batchId":"RC_10"} {"@serialNumber":"400003262400", "@timestamp":"20150721T10:22:59+0530" +, "@subscriberId":"9675312531", "@transactionId":"442230856161952", " +@operatorId":"", "@voucherGroup":"A9", "@expiryDate":"20160817T23:59: +59+0530", "@state":"USED", "@reserveTimeoutResolution":"", "@subState +":"", "@serviceType":"COMMIT_RECORD", "@value":"777", "@agent":"", "@ +batchId":"RC_10"} {"@serialNumber":"235666366547", "@timestamp":"20150721T10:22:59+0530" +, "@subscriberId":"9759261690", "@transactionId":"732696256161800", " +@operatorId":"", "@voucherGroup":"B9", "@expiryDate":"20160731T23:59: +59+0530", "@state":"RESERVED", "@reserveTimeoutResolution":"PENDING", + "@subState":"", "@serviceType":"RESERVE_RECORD", "@value":"1554", "@ +agent":"", "@batchId":"10002"}

This looks like in proper format. I am not able to figure out the problem. Could you please help.

Replies are listed 'Best First'.
Re: Perl script not working with JSON module
by GotToBTru (Prior) on Jul 23, 2015 at 12:53 UTC

    The error does not say line 193, it says character 193. The problem with the file is near the beginning.

    Dum Spiro Spero
      He's parsing each line of the file as a separate JSON chunk, so the problem could be anywhere. Add print ">$line<\n" before the decode.
Re: Perl script not working with JSON module
by roboticus (Chancellor) on Jul 23, 2015 at 13:45 UTC

    Ankur_kuls:

    In addition to the error that GotToBTru noted, the part of the JSON you *do* show also has errors: you're missing commas between the } and { (i.e., at the ends of the lines)--assuming that you're in an array of hashes. (Though I can't imagine what else it would be.)

    I'd suggest talking to the provider of this "JSON" data, and have them provide you actual JSON data.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Hi.. thanks for help. I have resolved the issue now. Earlier I thought that issue lies within line number 193. Then I decoded each line in while loop with "$." which gave me the malformed record number where the script terminated. thanks roboticus, gottobtru and anonymous Monk for your time & help.