1

Okay, I have this method:

sub config {
my $filename = 'perl_config.txt';
my $json_text = do {
    open(my $json_fh, "<:encoding(UTF-8)", $filename)
        or die("Can't open the file!");
    local $/;
    <$json_fh>
};
my $json = JSON->new;

my $data = $json->decode($json_text);

};

Terminal (interpreter rather ;) ) says:

'"' expected, at character offset 115 (before "}") at ./proj_perl.pl line 25

Line 25 indicates on my $data = $json->decode($json_text);

EDIT: I've edited my question and cut unnecessary code. I still don't know what's wrong. My JSON file is:

{
"local_host": "localhost",
"local_port": "6000",
"save_dir": "received_files",
"my_data": "my_data.txt",
}
4
  • Show your JSON file. It's probably malformed. Commented Oct 28, 2020 at 0:58
  • Definitely malformed :) Commented Oct 28, 2020 at 1:29
  • { "local_host": "localhost", "local_port": "6000", "save_dir": "received_files", "my_data": "my_data.txt", } Commented Oct 28, 2020 at 8:47
  • 4
    Remove the final comma (after "my_data.txt") Commented Oct 28, 2020 at 9:27

1 Answer 1

4

This is not valid JSON:

{
"local_host": "localhost",
"local_port": "6000",
"save_dir": "received_files",
"my_data": "my_data.txt",
}

JSON forbids the final comma. Javascript allows it (I think?), but JSON is based on a subset of Javascript.

JSON::PP and JSON::XS (the two backends supported by the Perl JSON module) each have a relaxed mode where they relax a few of JSON's syntactic restrictions, including this one.

my $json = JSON->new->relaxed(1);

So either fix your JSON or use relaxed mode to parse it.

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

Comments

Your Answer

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