I'm trying to POST some JSON data to my Perl script, but the JSON module seems to be unable to cope with this string:
[{"":"#","jednostka":"","login":"SA"}]
I used encodeURIComponent()
in JavaScript, so my string actually looks like this:
[{%22%22:%22#%22,%22jednostka%22:%22%22,%22login%22:%22SA%22}]
Error log:
JSON::PP::decode_error('unexpected end of string while parsing JSON string') called at C:\strawberry\perl\lib/JSON/PP.pm line 837
| JSON::PP::string() called at C:\strawberry\perl\lib/JSON/PP.pm line 960
| JSON::PP::object() called at C:\strawberry\perl\lib/JSON/PP.pm line 724
| JSON::PP::value() called at C:\strawberry\perl\lib/JSON/PP.pm line 907
| JSON::PP::array() called at C:\strawberry\perl\lib/JSON/PP.pm line 725
| JSON::PP::value() called at C:\strawberry\perl\lib/JSON/PP.pm line 688
| JSON::PP::PP_decode_json('JSON::PP=HASH(0xd0c2534)', '[{%22%22:%22#%22,%22jednostka%22:%22%22,%22login%22:%22SA%22}]', 0) called at C:\strawberry\perl\lib/JSON/PP.pm line 148
I'm trying to POST some JSON data to my Perl script, but the JSON module seems to be unable to cope with this string:
[{"":"#","jednostka":"","login":"SA"}]
I used encodeURIComponent()
in JavaScript, so my string actually looks like this:
[{%22%22:%22#%22,%22jednostka%22:%22%22,%22login%22:%22SA%22}]
Error log:
JSON::PP::decode_error('unexpected end of string while parsing JSON string') called at C:\strawberry\perl\lib/JSON/PP.pm line 837
| JSON::PP::string() called at C:\strawberry\perl\lib/JSON/PP.pm line 960
| JSON::PP::object() called at C:\strawberry\perl\lib/JSON/PP.pm line 724
| JSON::PP::value() called at C:\strawberry\perl\lib/JSON/PP.pm line 907
| JSON::PP::array() called at C:\strawberry\perl\lib/JSON/PP.pm line 725
| JSON::PP::value() called at C:\strawberry\perl\lib/JSON/PP.pm line 688
| JSON::PP::PP_decode_json('JSON::PP=HASH(0xd0c2534)', '[{%22%22:%22#%22,%22jednostka%22:%22%22,%22login%22:%22SA%22}]', 0) called at C:\strawberry\perl\lib/JSON/PP.pm line 148
Share
Improve this question
edited Jul 12, 2016 at 7:45
ThisSuitIsBlackNot
24.1k9 gold badges65 silver badges111 bronze badges
asked Jul 12, 2016 at 5:54
Piotr BielskiPiotr Bielski
1051 gold badge2 silver badges8 bronze badges
3
-
How are you parsing the JSON? Have you tried
JSON.parse('[{"":"#","jednostka":"","login":"SA"}]')
? – Krishnakumar_Muraleedharan Commented Jul 12, 2016 at 5:58 - I'm not sure why you need the encodeURIComponent(). doesn't it work without it? and shouldn't you do decode on the other side? – Ronen Ness Commented Jul 12, 2016 at 6:14
- I can't reproduce the problem, you need to provide an minimal reproducible example – Quentin Commented Jul 12, 2016 at 6:54
3 Answers
Reset to default 3Do you realise that you ned to escape the data only for GET requests? That's why the function is called encodeURIComponent
, because the resulting string is for use within a URL. In POST requests the data is passed in the message body, not in the URL
I see that you're using the JSON::PP
module, which works fine with the simple JSON string that you show. Here's an example program that shows the resulting data structure dumped using Data::Dump
Acccording to the error log you're encoding the JSON data when you shouldn't be. If I replace the JSON data below with encodeURIComponent
then I get the error message
unexpected end of string while parsing JSON string, at character offset 3 (before "22%22:%22#%22,%22jed...")
which is as you described, and what the error log confirms
use strict;
use warnings 'all';
use JSON::PP;
use Data::Dump;
my $json = '[{"":"#","jednostka":"","login":"SA"}]';
my $data = JSON::PP::decode_json($json);
dd $data;
output
[{ "" => "#", "jednostka" => "", "login" => "SA" }]
Another way to get the "unexpected end of string while parsing JSON string" from Perl's JSON::decode_json() function, is if your JSON file contains trailing mas, like this:
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500,
} }}
The JSON specification does not allow a trailing ma (this is unrelated to URI-encoding, btw). You should be able to catch such JSON format violations using JSON::Parse::valid_json().
It looks like you're decoding it in Perl, but attempting to decode an empty variable name. Give the first variable a name.