Catman...getJSON(3pm)
β¨ Catmandu::Importer::getJSON β¨
okay bestie, welcome to the manual page for Catmandu::Importer::getJSON (it lives in the 3pm section, and yeah, it's a User Contributed Perl Documentation situation π
). This is the thing that goes and fetches JSON-encoded data off a server using a GET HTTP request. It's giving glow up for your data pipeline. Periodt.
NAME π
Catmandu::Importer::getJSON β load JSON-encoded data from a server using a GET HTTP request π«Ά
SYNOPSIS πΈ
the following three examples are equivalent, no cap:
Catmandu::Importer::getJSON->new(
file => \"http://example.org/alice.json\nhttp://example.org/bob.json"
)->each(sub { my ($record) = @_; ... );
Catmandu::Importer::getJSON->new(
url => "http://example.org",
file => \"/alice.json\n/bob.json"
)->each(sub { my ($record) = @_; ... );
Catmandu::Importer::getJSON->new(
url => "http://example.org/{name}.json",
file => \"{\"name\":\"alice\"}\n{\"name\":\"bob\"}"
)->each(sub { my ($record) = @_; ... );
and for more convenience you can just use the catmandu command line client, because obviously:
echo http://example.org/alice.json | catmandu convert getJSON to YAML
catmandu convert getJSON --from http://example.org/alice.json to YAML
catmandu convert getJSON --dry 1 --url http://{domain}/robots.txt < domains
DESCRIPTION π¦
so this Catmandu::Importer performs a HTTP GET request to load JSON-encoded data from a server. The importer expects a line-separated input, okay? Each line corresponds to a HTTP request that is mapped to a JSON-record on success. Obsessed.
the following input formats are accepted:
- plain URL β a line that starts with
"http://"or"https://"is used as plain URL. simple. iconic. - URL path β a line that starts with
"/"is appended to the configuredurlparameter. - variables β a JSON object with variables to be used with an URL template or as HTTP query parameters. For instance the input line
{"name":"Karl Marx"}with URLhttp://api.lobid.org/personor the input line{"entity":"person","name":"Karl Marx"}with URL templatehttp://api.lobid.org/{entity}{?id}{?name}{?q}are both expanded to <http://api.lobid.org/person?name=Karl+Marx>. like, the way it just builds that URL? chef's kiss π
ngl this next part matters: if the JSON data returned in a HTTP response is a JSON array, its elements are imported as multiple items. If a JSON object is returned, it is imported as one item. that's the whole vibe check right there.
CONFIGURATION π
urlβ an URI or an URI templates (URI::Template) as defined by RFC 6570 <http://tools.ietf.org/html/rfc6570> to load JSON from. If nourlis configured, plain URLs must be provided as input or optionfrommust be used instead. (no url, no problem β but you gotta give it something bestie.)fromβ a plain URL to load JSON without reading any input lines. set it and forget it β¨timeout/agent/proxy/headersβ optional HTTP client settings. the little knobs for your HTTP client πclientβ instance of a Furl HTTP client to perform requests with.dryβ don't do any HTTP requests but return URLs that data would be queried from. window shopping, essentially ποΈfile/fhβ input to read lines from (see Catmandu::Importer). defaults toSTDIN.fixβ an optional fix to be applied on every item (see Catmandu::Fix). glow-up pass on each record πwaitβ number of seconds to wait between requests. be polite to the server, bestie π«Άcacheβ cache JSON response of URLs to not request the same URL twice. HTTP error codes in the 4xx range (e.g.404) are also cached but 5xx errors are not.
The value of this option can be any objects that implements method get and set (e.g. CHI), an existing directory for file caching, a true value to enable global in-memory-caching, or a false value to disable caching (default).
File caching uses file names based on MD5 of an URL so for instance http://example.org/ is cached as 4389382917e51695b759543fdfd5f690.json. literally a fingerprint for your URL, iykyk π₯
warnβ show error messages on the standard error. the "hey bestie something's off" setting π
METHODS π
time β°
Returns the UNIX timestamp right before the last request. This can be used for instance to add timestamps or the measure how fast requests were responded. stopwatch energy fr fr β±οΈ
construct_url( [ $base_url, ] $vars_url_or_path ) π
Returns an URL given a hash reference with variables, a plain URL or an URL path. The optional first argument can be used to override option url.
$importer->construct_url( %query_vars )
$importer->construct_url( $importer->url, %query_vars ) # equivalent
like, same thing, two ways, pick your fave π
request($url) π‘
Perform a HTTP GET request of a given URL including logging, caching, request hook etc. Returns a hash/array reference or undef.
EXTENDING π§
This importer provides two methods to filter requests and responses, respectively. See Catmandu::Importer::Wikidata for an example. very main character of it to be extensible, honestly π
request_hook π
Gets a whitespace-trimmed input line and is expected to return an unblessed hash reference, an URL, or undef. Errors are catched and treated equal to undef. no drama, errors just quietly become undef π
response_hook πΈ
Gets the queried response object and is expected to return an object.
LOGGING π
URLs are emitted before each request on DEBUG log level. so if you're wondering "wait what did it even fetch" β crank up DEBUG, bestie π
LIMITATIONS β οΈ
Future versions of this module may also support asynchronous HTTP fetching modules such as HTTP::Async, for retrieving multiple URLs at the same time. so likeβ¦ not a thing yet. don't count on it, it's a maybe, it's giving soonβ’ π
SEE ALSO π
Catmandu::Fix::get_json provides this importer as fix function. same vibes, fix-flavoured π
COPYRIGHT AND LICENSE π
Copyright Jakob Voss, 2014-
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page bestie!! this one's from perl v5.36.0, dated 2022-12-10, and it lives in the Catman...getJSON(3pm) corner of the world. mwah π go fetch some JSON, you got this π«Άβ¨
π¦ where this page comes from
- debian bookworm: libcatmandu-importer-getjson-perl 0.52-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libcatmandu-importer-getjson-perl 0.52-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcatmandu-importer-getjson-perl 0.52-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu questing: libcatmandu-importer-getjson-perl 0.52-2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms