Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8260595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:08:06+00:00 2026-06-08T03:08:06+00:00

I’m working on a perl script that parses reddit’s JSON using the JSON module.

  • 0

I’m working on a perl script that parses reddit’s JSON using the JSON module.

However I do have the problem of being very new to both perl and json.

I managed to parse the front page and subreddits successfully, but the comments have a different structure and I can’t figure out how to access the data I need.

Here’s the code that successfully finds the “data” hash for the front page and subreddits:

foreach my $children(@{$json_text->{"data"}->{"children"}}) #For values of children.
{
    my $data = $children->{"data"}; #accessing each data hash.
    my %phsh = ();                  #my hash to collect and print.

            $phsh{author} = $data->{"author"};#Here I get the "author" value from "data"
*Etc....

This successfully gets what I need from http://www.reddit.com/.json

But when I go to the json of a comment, this one for example, it has a different format and I can’t figure out how to parse it. If I try the same thing as before my parser crashes, saying it is not a HASH reference.

So my question is: How do access the “children” in the second JSON? I need to get both the data for the Post and the data for the comments. Can anybody help?

Thanks in advance!
(I know it may be obvious, but I’m running on very little sleep XD)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:08:07+00:00Added an answer on June 8, 2026 at 3:08 am

    You need to either look at the JSON data or dump the decoded data to see what form it takes. The comment data, for example is an array at the top level.

    Here is some code that prints the body field of all top-level comments. Note that a comment may have an array of replies in its replies field, and each reply may also have replies in turn.

    Depending on what you want to do you may need to check whether a reference is to an array or a hash by checking the value returned by the ref operator.

    use strict;
    use warnings;
    
    binmode STDOUT, ':utf8';
    
    use JSON;
    use LWP;
    use Data::Dump;
    
    my $ua = LWP::UserAgent->new;
    my $resp = $ua->get('http://www.reddit.com/r/funny/comments/wx3n5/caption_win.json');
    die $resp->status_line unless $resp->is_success;
    
    my $json = $resp->decoded_content;
    my $data = decode_json($json);
    
    die "Error: $data->{error}" if ref $data eq 'HASH' and exists $data->{error};
    
    dd $data->[1]{data}{children}[0];
    print "\n\n";
    
    my $children = $data->[1]{data}{children};
    print scalar @$children, " comments:\n\n";
    
    for my $child (@$children) {
      print $child->{data}{body}, "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.