Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a https JSON stream with perl?

Tags:

json

https

perl

lwp

Ok I'll try and be as detailed as possible.

I need to connect to a https url that streams JSON data, and search for keywords via REGEX as the data streams then take the matching JSON element decode it and insert it into a database.

The REGEX on is not the problem that's relatively simple.

What I'm struggling with is reading the data line wise. I've tried a few examples I found online the use LWP but where the stream never stops loading the script hangs.

Here's the closest I've gotten

#!/usr/bin/perl

use strict;
use warnings;
use LWP::Simple;
use IO::String;

my $handle = IO::String->new(get("https://stackoverflow.com"));
while (defined (my $line = <$handle>)) {
  print $line; #Inserted for testing

  #Decode and insert into DB here       
}
close $handle;

The data comes in a fairly fast rate so the script has to be efficient.

Any pointers as to how to get this done would be great.

Thanks Sean

like image 242
Sean Smith Avatar asked Jan 26 '26 05:01

Sean Smith


1 Answers

JSON::SL can help you.

Also see Chas. Owens' answer where he compares JSON::SL and JSON::Streaming::Reader.

like image 81
Chankey Pathak Avatar answered Jan 27 '26 21:01

Chankey Pathak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!