Sample Header Ad - 728x90

Getting the first/last/nth result from a jq select result

2 votes
1 answer
8986 views
I am trying to find a way to pipe the result of my current script into another command which will leave me with just the last result of the array I get in my select here:
jq -r --arg name "$1" '.packageAliases | to_entries[] | select(.key | startswith($name))' sfdx-project.json
the sfdx-project.json file is a json file that has a nested JSON called packageAliases, which a script adds new package version numbers and Ids to.
{
 "data" : "that",
 "is" : "not",
 "really" : "necessary",
 "packageAliases" : {
  "package" : "0H0fffff",
  "package@0.1.0.1" : "04t0xxxxxx"
 }
}
The idea is to select all package aliases of a specific package name, and leave me with the last one, which is the one that I am supposed to use. When I try to pipe the result I get from my select into last, I get this error: Cannot index object with number Although I thought that the select() command returns an array (which I also get when I echo the intermediary result) Placing the last() command anywhere else just leaves me with several null values in an array, but I still do not get the single value I actually need. What do I need to change about my command to get the last entry from the array that I get out of select()?
Asked by dschib (51 rep)
Mar 9, 2022, 02:09 PM
Last activity: Mar 9, 2022, 06:52 PM