Search code examples
jsonperlhttpslwp

How to read a https JSON stream with perl?


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


Solution

  • JSON::SL can help you.

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