in reply to Append JSON (from hashref)

Don't add it to the JSON, add it to $tmp.

Alternatively, consider selecting the information directly from the database.

Replies are listed 'Best First'.
Re^2: Append JSON (from hashref)
by Anonymous Monk on Apr 06, 2017 at 11:34 UTC
    So the existing JSON looks like
    [{"DESCRIPTION":"Description of record","RECNO":"RECORDNumber"}, ... m +ore records here ...]
    the database query is
    SELECT RECNO, DESCRIPTION FROM RECORDS_TABLE WHERE RECNO LIKE ?
    Called with
    $dbh->selectall_arrayref($query, { Slice => {} }, $SearchWord );
    Is there a way I can change the query to return a structure required by this plugin, while including the additional field
    "inputPhrase": $SearchWord

      Yes.

      See perldsc on how to manipulate Perl data structures.

      You could either add your entry to the result directly, in your Perl code:

      $tmp->[0]->{ 'inputPhrase' } = $SearchWord;

      ... or simply select the search phrase as another column:

      SELECT RECNO, DESCRIPTION, ? as inputPhrase FROM RECORDS_TABLE WHERE RECNO LIKE ?
      ... $sth->execute( $SearchWord, $recno ); ...
        Thanks, but this first solution means inputPhrase is added in with the details of the first result
        [{"DESCRIPTION":"Description of record","RECNO":"RECORDNumber", "input +Phrase":"RECORDNumber}, ... more records here
        and not outside of the list of results like in the json linked above (won't let me post with the URL here)

      like Corion said, add it to $tmp

      $tmp->{inputPhrase}=$SearchWord;
      Then it will be converted to json just fine.

      Edit: Now im not paying enough attention, The hash is inside an array at $tmp->[0] See Re^3: Append JSON (from hashref)