Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQ - How to define filter to remove brackets, quotes and commas from output array

Tags:

git

output

xargs

jq

I need to convert an output array to lines without brackets, quotes and commas, so that it can be used to create git clones.

This is my original query

curl -s http://bitbucketlocalserver:7990/rest/api/1.0/projects/PROJECT_NAME/repos?per_page=20 ^
-u user:pass | H:\Downloads\Win64\jq-win64.exe -r "[.values[] | ((.links.clone[] | select(.name==\"http\") | .href)  + \" \" +  .name)]"  

which returns an output of the format

[
   "http://bitbucketlocalserver:7990/scm/PROJECT_NAME/gitrepo1.git GitRepository1",
   "http://bitbucketlocalserver:7990/scm/PROJECT_NAME/gitrepo1.git GitRepository2"
]

I want to use the output as input to another command like below

 curl -s http://bitbucketlocalserver:7990/rest/api/1.0/projects/PROJECT_NAME/repos?per_page=20 ^
-u user:pass | H:\Downloads\Win64\jq-win64.exe -r "[.values[] | ((.links.clone[] | select(.name==\"http\") | .href)  + \" \" +  .name)]"   | ^
 H:\Utilities\Git\usr\bin\xargs.exe -n 2 git clone -b release-dev

To be able to use this command, the output of the jq command needs to be like this

http://bitbucketlocalserver:7990/scm/PROJECT_NAME/gitrepo1.git GitRepository1
http://bitbucketlocalserver:7990/scm/PROJECT_NAME/gitrepo1.git GitRepository2

The first part is part of this link

What changes do I need to make to the JQ filter so that I can perform this? In reality I need to clone more than 40 repositories from the BitBucket project and I would like to create a simple script where I do not have to get the list first.

like image 405
adbdkb Avatar asked Jan 26 '23 16:01

adbdkb


1 Answers

You could simply tack on the following jq filter to the filter that produces the array of two strings:

.[]

Or more economically, simply remove the outer square brackets from the jq query.

The point is that the -r option only strips the double-quotation marks on outputs that are JSON strings (not strings within compound entities).

like image 120
peak Avatar answered Jan 28 '23 10:01

peak