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

hi, i am generating a json file which will have all possible cases that a json object could hold and i want the object to be as large as 64kb and i am manually appending and forming the json object,but to validate the formed string i used from_json($string), i am getting the errors i could not find where the error occurs,please help me

sub json() { my $obj="["; my $n=20; for(my $i=0;$i<$n;$i++) { $obj.="{ \"id\":\"stu_$i\", "; $obj.="\"is Active\":true,\"name\":\"studentname$i\","; $obj.="\"address\":".getaddr(); $obj.="\"qualifications\":".getqualifications(); my $k=int(rand(20))+20; $obj.="\"age\":$k,"; $obj.="\"gender\":\"male\","; $obj.="\"email\":".getemail(); $obj.="\"about\":".getabout(); $obj.="\"rgistered\":\"".localtime()."\","; $obj.="\"phone\":".getphone(); $obj.=getstandctry(); $obj.="\"subjects\":".getsubjects().","; $obj.="\"subjects2\":".getsubjects(); $obj.="},"; } chop($obj) if $obj=~ m(,$); #if (is_json_valid($obj)) # { # print " ... do something ..."; # } my $data_structure = from_json($obj); print Dumper($data_structure) ; writetofile($data_structure); }

error: , or ] expected while parsing array, at character offset 658656 (before "(end of string)") at C:/Strawberry/perl/vendor/lib/JSON.pm line 168. Press any key to continue . . .

Replies are listed 'Best First'.
Re: need help in validating json
by Loops (Curate) on Oct 27, 2014 at 07:23 UTC

    Hi praveenchappa and welcome to the monastery.

    Without seeing all of the code and input data, it'll be hard for anyone to debug. There are some good tips above about how you should go about debugging yourself. But getting all the quoting and escaping correct is likely where the problem is. It will be much easier on you if you use one of the CPAN modules like JSON or JSON::XS which can take a perl hash as input and encode json strings for you.

Re: need help in validating json
by poj (Abbot) on Oct 27, 2014 at 08:21 UTC
    #!perl use strict; use JSON; use Data::Dump 'pp'; my $obj = json(20); pp $obj; print "\n"; my $data_structure = from_json($obj); pp $data_structure ; sub json { my $n = shift; my @ar=(); for my $i (1..$n){ my $record = { 'id' => "stu_$i", 'is Active' => 'true', 'name' => "studentname$i", 'address' => 'getaddr', 'qualifications' => 'getqualifications', 'age' => 20+int rand(20), 'gender' => 'male', 'email' => 'getemail', 'about' => 'getabout', 'rgistered' => scalar localtime(), 'phone ' => 'getphone', 'standctry' => 'getstandctry', 'subjects' => 'getsubjects', 'subjects2' => 'getsubjects', }; push @ar,$record; } return to_json(\@ar); }
    poj
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: need help in validating json
by Corion (Patriarch) on Oct 27, 2014 at 08:06 UTC

    Have you looked at whether your code works for a single object instead of 20 objects?

    As a simple example, I don't see where you output the closing ] for your array of objects.

    If you're using from_json to validate your output, why don't you use to_json to create your JSON output? At least for testing and comparing the created data, this seems to me a much easier approach to creating correct data.

Re: need help in validating json
by Anonymous Monk on Oct 27, 2014 at 07:16 UTC
Re: need help in validating json
by Random_Walk (Prior) on Oct 27, 2014 at 11:07 UTC

    Try pasting your JSON into an online validator. I use: http://jsonlint.com/ that give a more useful idea of where it is all going pear shaped.

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!