1

JSON RESPONSE: Below is my json response which i am to parse from url.

[
    {
        "StationID": 17682,
        "TVEStationID": 0,
        "CallLetters": "DIYHD",
        "StationName": "DIYHD",
        "Affiliation": "Do-It-Yourself Network HD",
    },
    {
        "StationID": 17683,
        "TVEStationID": 0,
        "CallLetters": "WGNA",
        "StationName": "WGNA",
        "Affiliation": "WGN America HD",
    }
]

Perl code :

my $response = $ua->get('https://myurl here......');

if ($response->is_success) {
    print $response->decoded_content;
}

I have to parse above json and store it into a variable for all five values, I am new in perl not able to find out the solution for this, Please help me to parse this.

Here, I am getting success response but inside the response how i have to parse and get the value... from array that i am not able to do.

Thankyou

5
  • What is the get() function you're calling on $response? What output are you getting? And what exactly are you struggling with? Commented Jun 29, 2020 at 9:24
  • my response is coming , now i have edited my question. In response i am getting my json response, now how i have to parse please help me. @simbabque Commented Jun 29, 2020 at 9:38
  • @PrityKumari: So here my $response = $ua->get('https://myurl here......'); you're getting the JSON response and you need to store in what? Parse it in which format? Shall we know how your output should look like from the response you're getting? Commented Jun 29, 2020 at 9:47
  • I have to store all values into variable insert into table, I am not able to irritate for loop and not able to get values, please help me @vkk05 Commented Jun 29, 2020 at 9:59
  • 1
    @PrityKumari: Have a look into Dave's answer and print Dumper for $data. You will get response from the URL which is been mentioned in $ua->get(..) in very nice format. From there you can take forward how to insert it in table. You can open separate thread if parsing from $data becomes difficult. Commented Jun 29, 2020 at 15:13

2 Answers 2

2

Use a library, my preferred one is Cpanel::JSON::XS, but see also JSON::MaybeXS, JSON::XS, and JSON::PP.

#! /usr/bin/perl
use strict;
use warnings;

...
use Cpanel::JSON::XS;

my $response = $ua->get('https://myurl here......');
if ($response->is_success) {
    my $structure = decode_json($response->decoded_content);
    print $structure->[1]{StationID};  # 17683
}

Note that I had to remove the trailing commas from the JSON.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi @choroba I have to parse from URL , Please help me on that.
Just try calling decode_json on the $json variable in your code.
if ($response->is_success) { my $structure1 = decode_json($response); print $structure1->[1]{StationID}; I am using like above code , please check once.
But I have to parse fom API json response , not from only json
I don't understand. Please give more details.
|
1

You want something like this:

use JSON;

my $response = $ua->get('https://myurl here......');

if ($response->is_success) {
  my $json = $response->decoded_content;

  my $data = JSON->new->decode($json);

  # $data now contains an array reference which contains
  # all of your data.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.