Sec has asked for the wisdom of the Perl Monks concerning the following question:
Hi. I'm looking for a simple way to parse JSON objects out of a stream.
I am calling an HTTP service that returns a stream of json objects over time.
To read it, i registered a simple callback handler in LWP::UserAgent
$ua->add_handler( response_data => sub { my($response, $ua, $h, $data) = @_; return "true" if $data =~ /^\s*$/; print "Callback triggered\n"; print "data: .${data}.\n"; my $result = eval { $json->decode( $data ) }; if ($@){ print "error: $@\n"; return "true"; }; print Dumper $result; return "true"; } );
As expected, i sometimes get partial json objects in $data.
My first idea would be to so something like m/{.*?}/ against $data. Or rather some more elaborate version that deals with balanced parens and quoted strings.
My question is, is there an easier / simpler / more straightforward way to deal with something like this that I am missing?
-- SecEDIT: to clarify a bit.
The problem is that in the callback $data may just be
which means I have to grab one object, handle it, keep the rest and wait for more data.{ "foo": "bar" } { "foo" : "q
I know how to write code for that "the long way". I was asking if there is something clever or an existing module that can help with this.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Parsing JSON out of an incremental stream
by huck (Prior) on Feb 15, 2019 at 17:40 UTC | |
by Sec (Monk) on Feb 18, 2019 at 13:13 UTC | |
by haj (Vicar) on Feb 18, 2019 at 13:36 UTC | |
by Sec (Monk) on Feb 18, 2019 at 16:00 UTC | |
by marto (Cardinal) on Feb 18, 2019 at 16:26 UTC | |
Re: Parsing JSON out of an incremental stream
by Sec (Monk) on Feb 15, 2019 at 17:52 UTC | |
by Your Mother (Archbishop) on Feb 15, 2019 at 18:53 UTC | |
by Sec (Monk) on Feb 18, 2019 at 17:40 UTC |