0

how can i parse perl json object which has spaces in its keys

{
   "abc" : [
       "lmn" : {
          "Ab Cd" : "Xy Zw",
          "Ef Gh" : "Pq Rs",
       }
   ]
}
Alter16
  • 9
  • 5

2 Answers2

6

By definition, one parses JSON using a JSON parser. There exists multiple JSON parsers on CPAN, including Cpanel::JSON::XS. It handles keys with spaces in them without issue, as should every other JSON parser.

Note that what you have isn't JSON. I'm assuming the errors are typos since you asked about JSON.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • XS can do the job but is it possible with PP?? (that comma is a typo) – Alter16 Mar 05 '20 at 09:02
  • Much slower, but yes – ikegami Mar 05 '20 at 09:07
  • Can you tell me how because $VAR1 -> {abc} -> {lmn} -> {Ab Cd} this gave me an Can't locate object method "Ab" via package "Cd" – Alter16 Mar 06 '20 at 05:26
  • @Alter16, `$VAR1 -> {abc} -> {lmn} -> {Ab Cd}` should be `$VAR1 -> {abc} -> {lmn} -> {'Ab Cd'}`. You can only omit the quotes around the key expression if it's a "word". – ikegami Mar 06 '20 at 05:29
  • Yessss for 'Ab Cd' i got Use of uninitialized value error – Alter16 Mar 06 '20 at 05:32
  • You get that error when you have `undef`, which could be because you had `null` in the JSON, or because you are referencing a non-existent array or hash element. – ikegami Mar 06 '20 at 05:36
0

Spaces in a key will present no problems at all to any JSON parser.

There are, however, two problems in your JSON that will cause problems for any parser. Others have noted the extra comma after "Pq Rs", but you also have an array that contains a key/value pair (with the key "lnm") which needs to be inside an object.

Originally, I just removed the comma and ran this code:

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use Data::Dumper;
use JSON;

my $json = '{
   "abc" : [
       "lmn" : {
          "Ab Cd" : "Xy Zw",
          "Ef Gh" : "Pq Rs"
       }
   ] 
}';

my $data = decode_json($json);

say Dumper $data;

This gives an error:

, or ] expected while parsing array, at character offset 28 (before ": {\n "Ab C...")

I fixed it, by inserting { ... } around the lnm object.

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use Data::Dumper;
use JSON;

my $json = '{
   "abc" : [ {
       "lmn" : {
          "Ab Cd" : "Xy Zw",
          "Ef Gh" : "Pq Rs"
       }
   } ] 
}';

my $data = decode_json($json);

say Dumper $data;

And then I got this output:

$VAR1 = {
          'abc' => [
                     {
                       'lmn' => {
                                  'Ab Cd' => 'Xy Zw',
                                  'Ef Gh' => 'Pq Rs'
                                }
                     }
                   ]
        };

Which is, I think, what you are expecting.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Thank you for your helpful observation but i had an issue while getting value for $VAR1 -> {abc} -> {lmn} -> {'Ab Cd'} – Alter16 Mar 05 '20 at 14:26
  • @Alter16: What does "I had an issue" mean? It's not very clear. Please add the details to your question. – Dave Cross Mar 05 '20 at 15:12