2

I have a script I have been using where a user will enter a search string and it will then return the json result of the search. I however had to make changes and let the script search for the data in a table and print a json result. I created a for loop to use perform this task. (posted a sample of the for loop which is the only relevant part here):

#!/bin/perl
use strict;
use warnings;
use JSON;
$json = JSON->new->allow_nonref;
...
foreach (...) {
      my $object = {name => "$name", surname => "$surname", age => "$age"}, 'response';
      my $result = $json->encode($object);
      print "$result";
  }

This does exactly what it use to and prints the each element in valid json format (manually made it pretty):

{
   "name" : "Sarah",
   "surname" : "O'Conner",
   "age" : "89"
}
{
   "name" : "John",
   "surname" : "Smith",
   "age" : "32"
}

The problem is that each each json elements are valid, but invalid as multiple root elements. I instead needed this:

[
   {
      "name":"Sarah",
      "surname":"O'Conner",
      "age":"89"
   },
   {
      "name":"John",
      "surname":"Smith",
      "age":"32"
   }
]

I tried 20 different ways but I just cannot fix this. Can anyone please help me with fixing this? How do I get the result as a multiple root element and separate elements?

5
  • Build an array of your hashrefs and encode a reference to it? Commented Aug 5, 2021 at 8:44
  • Also, why do you need to run bless? Commented Aug 5, 2021 at 9:20
  • @Shawn, Thanks for the reply. I will need to read up on how to do that. Commented Aug 5, 2021 at 9:22
  • @choroba. I honestly do not know, I only used an example from the metacpan page and it worked for the single json results. Commented Aug 5, 2021 at 9:23
  • @choroba, Thanks, I amended the code to not bless anymore. Now just trying to figure out how to get the desired result. Commented Aug 5, 2021 at 10:21

2 Answers 2

4

Create an array of the "objects" (called hashes in Perl) and encode it:

my @objects;
foreach (...) {
      push @objects, {name => $name, surname => $surname, age => $age};
}
my $result = $json->encode(\@objects);
print $result;

If you show us the ... part, we might show you how to translate it to @objects directly without pushing (e.g. using a map).

BTW, there's no need to doublequote variables.

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

1 Comment

Thanks a mill. See also my comment about this to @DaveCross. So about the foreach (...) question in your answer. In my actual code I actually get information from various for loops which gets the results from different application API's and DB tables. So the methods will derive from some commands, the idea is then to push the data to an array and print the entire result at the end of the run.
4

You're doing the JSON conversion at the wrong point of the process. Instead of converting each individual record, you should create an array of your records and convert that.

#!/bin/perl
use strict;
use warnings;
use JSON;
$json = JSON->new->allow_nonref;
...
my @objects;

foreach (...) {
      my $object = {name => "$name", surname => "$surname", age => "$age"};
      push @objects, $object;
}

my $result = $json->encode(\@objects);
print "$result";

1 Comment

Still has a dangling , 'response' -- maybe from removing bless()?

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.