I'm looking for an effective way to extract data from a JSON feed in a nice perl parser to dump values into variables for use. Feeds are separated line by line instead of an over-encompassing array, so it's throwing me off. Here is an example:
{ "1": {"subject1": "value", "subject2": [{"subject3": "value", "subject +4": "value"}], "subject5": "value", "subject6": value, "subject7": "v +alue"}, "2": {"subject1": "value", "subject2": [{"subject3": "value", "subject +4": "value"}], "subject5": "value", "subject6": value, "subject7": "v +alue"}, "3": {"subject1": "value", "subject2": [{"subject3": "value", "subject +4": "value"}], "subject5": "value", "subject6": value, "subject7": "v +alue"},
etc.... (there is a lot more, ignore malformed JSON!) So instead of something that has an all encompassing array reference such as
DATA [ "1": {"subject1": "value"}, "2": {"subject1": "value"} ]
Which would be easy where I can identify each subject's value by a format similar to the one below, it is just separated essentially by unique numbers line by line from {} which are not detecting the array. My older code had said something along the lines of:
my $jsonoutput = "Above raw JSON format..."; my $json = new JSON; my $jsontext = $json->allow_nonref->utf8->relaxed->escape_slash->l +oose->allow_singlequote->allow_barekey->decode("$jsonoutput"); foreach my $stuff(@{$json->{1}}){ my %hash = (); $hash{subject1} = $stuff->{subject1}; }
Doing this produces "Not an Array Reference". If I was to use "subject2" as oppsed to 1 from $json-> it does not error out, yet does not show subject 3, subject 4, etc's value.

In reply to Extract JSON data by omegaweaponZ

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.