I'm trying to use jq to look up data from a json file with account mappings. So given an account number as input, I want to look up it's name from a file, and it needs to handle the account not being found.
echo '["1100", "1350"]' | jq --slurpfile accountsdata accounts.json ' def get_account_data(account): [$accountsdata[] | .[] | select((.accountnumber | tostring) == (account | tostring))] as $output | if $output | length == 0 then {"name": ("no mapping for " + account +".")} else $output[0] end; def get_name(account): get_account_data(account).name;. | map(get_name(.))'
The account mapping file is as follows:
[ {"accountnumber": 1100,"name": "Account A" }, {"accountnumber": 1350,"name": "Account B" }, {"accountnumber": 1356,"name": "Account C" }]
Expected output:
["Account A","Account B"]
Actual output:
["no mapping for 1100.","no mapping for 1350."]
However, if I change the call from . | map(get_name(.))
to . | map(get_name("1100"))
(i.e. hardcode the accountnumber
), I get the expected
["Account A","Account A"]
What am I missing?