in reply to Re: Append JSON (from hashref)
in thread Append JSON (from hashref)

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

Replies are listed 'Best First'.
Re^3: Append JSON (from hashref)
by Corion (Patriarch) on Apr 06, 2017 at 11:39 UTC

    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)

        Ah, if you want a different structure, just create that structure.

        Consider writing your data structure in Perl - Perl looks very much like JSON, except that : is spelled => in Perl.

Re^3: Append JSON (from hashref)
by huck (Prior) on Apr 06, 2017 at 11:37 UTC

    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)