in reply to Extract JSON data

omegaweaponZ:

I'd suggest using JSON to parse out JSON items. Be aware, however, that the data you show in your example is not valid JSON. Be sure to arrange with the provider of the so-called JSON to have them correct it. The bits I see wrong are:

I don't know whether the commas are a problem or not, but the missing curly braces and missing quotes are problematic. Once you fix the data, the JSON package makes parsing trivial:

$ cat t.pl use strict; use warnings; use Data::Dumper; use JSON; my $text; { local $/;$text= <DATA> }; my $junk = decode_json($text); print Dumper($junk); __DATA__ { "1": { "subject1": "value", "subject2": [ { "subject3": "value", "su +bject4": "value" } ], "subject5": "value", "subject6": "value", "subj +ect7": "value" } } $ perl t.pl $VAR1 = { '1' => { 'subject7' => 'value', 'subject2' => [ { 'subject4' => 'value', 'subject3' => 'value' } ], 'subject5' => 'value', 'subject1' => 'value', 'subject6' => 'value' } };

...roboticus

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

Replies are listed 'Best First'.
Re^2: Extract JSON data
by Random_Walk (Prior) on May 19, 2013 at 11:00 UTC
    I don't know whether the commas are a problem or not, but the missing curly braces and missing quotes are problematic. Once you fix the data, the JSON package makes parsing trivial:

    The trailing commas are out of spec, but if you use the $json->relaxed([$enable]) then they will parse with no problems. relaxed also allows # comments in your JSON. Nice for config files.

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re^2: Extract JSON data
by omegaweaponZ (Beadle) on May 21, 2013 at 13:59 UTC
    I've updated the above a bit, I might not have explained it properly. Ignore the malformed brackets, this is just a sample code. The issue is when I place in the numerals of 1, 2, 3, etc, any underlying subject comes up as "Not an array reference". If I actually put in subject3, within its own array, that does not error out, but it also does not show me the values of what's within the array (subject 4, 5, etc). So the issue is I need to identify each subjects value, is my array incorrect or am I setting up the parameters incorrectly? Also, the JSON output cannot be changed
      I resolved this myself, please ignore. At this point, still need to store the numerals of 1,2,3, however into a variable from reading the json file