looping through JSON array in shell script
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
Below is the curl command output (file information about branch), need script or command to print file name , filetype and size.
I have tried with jq but was able fetch single value ( jq '.values.size')
"path":
"components": ,
"name": "",
"toString": ""
,
"revision": "master",
"children":
"size": 5,
"limit": 500,
"isLastPage": true,
"values": [
"path":
"components": [
".gitignore"
],
"parent": "",
"name": ".gitignore",
"extension": "gitignore",
"toString": ".gitignore"
,
"contentId": "c9e472ef4e603480cdd85012b01bd5f4eddc86c6",
"type": "FILE",
"size": 224
,
"path":
"components": [
"Jenkinsfile"
],
"parent": "",
"name": "Jenkinsfile",
"toString": "Jenkinsfile"
,
"contentId": "e878a88eed6b19b2eb0852c39bfd290151b865a4",
"type": "FILE",
"size": 1396
,
"path":
"components": [
"README.md"
],
"parent": "",
"name": "README.md",
"extension": "md",
"toString": "README.md"
,
"contentId": "05782ad495bfe11e00a77c30ea3ce17c7fa39606",
"type": "FILE",
"size": 237
,
"path":
"components": [
"pom.xml"
],
"parent": "",
"name": "pom.xml",
"extension": "xml",
"toString": "pom.xml"
,
"contentId": "9cd4887f8fc8c2ecc69ca08508b0f5d7b019dafd",
"type": "FILE",
"size": 2548
,
"path":
"components": [
"src"
],
"parent": "",
"name": "src",
"toString": "src"
,
"node": "395c71003030308d1e4148b7786e9f331c269bdf",
"type": "DIRECTORY"
],
"start": 0
expected output should be something like below
.gitignore FILE 224
Jenkinsfile FILE 1396
Thanks in advance :)
shell scripting json
New contributor
Sugatur Deekshith S N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
favorite
Below is the curl command output (file information about branch), need script or command to print file name , filetype and size.
I have tried with jq but was able fetch single value ( jq '.values.size')
"path":
"components": ,
"name": "",
"toString": ""
,
"revision": "master",
"children":
"size": 5,
"limit": 500,
"isLastPage": true,
"values": [
"path":
"components": [
".gitignore"
],
"parent": "",
"name": ".gitignore",
"extension": "gitignore",
"toString": ".gitignore"
,
"contentId": "c9e472ef4e603480cdd85012b01bd5f4eddc86c6",
"type": "FILE",
"size": 224
,
"path":
"components": [
"Jenkinsfile"
],
"parent": "",
"name": "Jenkinsfile",
"toString": "Jenkinsfile"
,
"contentId": "e878a88eed6b19b2eb0852c39bfd290151b865a4",
"type": "FILE",
"size": 1396
,
"path":
"components": [
"README.md"
],
"parent": "",
"name": "README.md",
"extension": "md",
"toString": "README.md"
,
"contentId": "05782ad495bfe11e00a77c30ea3ce17c7fa39606",
"type": "FILE",
"size": 237
,
"path":
"components": [
"pom.xml"
],
"parent": "",
"name": "pom.xml",
"extension": "xml",
"toString": "pom.xml"
,
"contentId": "9cd4887f8fc8c2ecc69ca08508b0f5d7b019dafd",
"type": "FILE",
"size": 2548
,
"path":
"components": [
"src"
],
"parent": "",
"name": "src",
"toString": "src"
,
"node": "395c71003030308d1e4148b7786e9f331c269bdf",
"type": "DIRECTORY"
],
"start": 0
expected output should be something like below
.gitignore FILE 224
Jenkinsfile FILE 1396
Thanks in advance :)
shell scripting json
New contributor
Sugatur Deekshith S N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Below is the curl command output (file information about branch), need script or command to print file name , filetype and size.
I have tried with jq but was able fetch single value ( jq '.values.size')
"path":
"components": ,
"name": "",
"toString": ""
,
"revision": "master",
"children":
"size": 5,
"limit": 500,
"isLastPage": true,
"values": [
"path":
"components": [
".gitignore"
],
"parent": "",
"name": ".gitignore",
"extension": "gitignore",
"toString": ".gitignore"
,
"contentId": "c9e472ef4e603480cdd85012b01bd5f4eddc86c6",
"type": "FILE",
"size": 224
,
"path":
"components": [
"Jenkinsfile"
],
"parent": "",
"name": "Jenkinsfile",
"toString": "Jenkinsfile"
,
"contentId": "e878a88eed6b19b2eb0852c39bfd290151b865a4",
"type": "FILE",
"size": 1396
,
"path":
"components": [
"README.md"
],
"parent": "",
"name": "README.md",
"extension": "md",
"toString": "README.md"
,
"contentId": "05782ad495bfe11e00a77c30ea3ce17c7fa39606",
"type": "FILE",
"size": 237
,
"path":
"components": [
"pom.xml"
],
"parent": "",
"name": "pom.xml",
"extension": "xml",
"toString": "pom.xml"
,
"contentId": "9cd4887f8fc8c2ecc69ca08508b0f5d7b019dafd",
"type": "FILE",
"size": 2548
,
"path":
"components": [
"src"
],
"parent": "",
"name": "src",
"toString": "src"
,
"node": "395c71003030308d1e4148b7786e9f331c269bdf",
"type": "DIRECTORY"
],
"start": 0
expected output should be something like below
.gitignore FILE 224
Jenkinsfile FILE 1396
Thanks in advance :)
shell scripting json
New contributor
Sugatur Deekshith S N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Below is the curl command output (file information about branch), need script or command to print file name , filetype and size.
I have tried with jq but was able fetch single value ( jq '.values.size')
"path":
"components": ,
"name": "",
"toString": ""
,
"revision": "master",
"children":
"size": 5,
"limit": 500,
"isLastPage": true,
"values": [
"path":
"components": [
".gitignore"
],
"parent": "",
"name": ".gitignore",
"extension": "gitignore",
"toString": ".gitignore"
,
"contentId": "c9e472ef4e603480cdd85012b01bd5f4eddc86c6",
"type": "FILE",
"size": 224
,
"path":
"components": [
"Jenkinsfile"
],
"parent": "",
"name": "Jenkinsfile",
"toString": "Jenkinsfile"
,
"contentId": "e878a88eed6b19b2eb0852c39bfd290151b865a4",
"type": "FILE",
"size": 1396
,
"path":
"components": [
"README.md"
],
"parent": "",
"name": "README.md",
"extension": "md",
"toString": "README.md"
,
"contentId": "05782ad495bfe11e00a77c30ea3ce17c7fa39606",
"type": "FILE",
"size": 237
,
"path":
"components": [
"pom.xml"
],
"parent": "",
"name": "pom.xml",
"extension": "xml",
"toString": "pom.xml"
,
"contentId": "9cd4887f8fc8c2ecc69ca08508b0f5d7b019dafd",
"type": "FILE",
"size": 2548
,
"path":
"components": [
"src"
],
"parent": "",
"name": "src",
"toString": "src"
,
"node": "395c71003030308d1e4148b7786e9f331c269bdf",
"type": "DIRECTORY"
],
"start": 0
expected output should be something like below
.gitignore FILE 224
Jenkinsfile FILE 1396
Thanks in advance :)
shell scripting json
shell scripting json
New contributor
Sugatur Deekshith S N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Sugatur Deekshith S N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 39 mins ago
New contributor
Sugatur Deekshith S N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 46 mins ago
Sugatur Deekshith S N
62
62
New contributor
Sugatur Deekshith S N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Sugatur Deekshith S N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Sugatur Deekshith S N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
Extracting the members
jq -c '.children.values|[.path.components[0],.type,.size]'
.children.values
outputs every member of the array.values
.|
pipes the previous result through the next filter, rather like a shell pipe[
...,
...,
...]
makes all the terms inside appear in a single array- The
-c
option produces "compact" format ie. one object per line
Result:
[".gitignore","FILE",224]
["Jenkinsfile","FILE",1396]
["README.md","FILE",237]
...
Formatting the result
If you want to output a neatly-aligned table, that's a task better handled by other tools, such as column
or paste
.
jq -c '.children.values|[.path.components[0],.type,.size]' | column -t -s',"'
-t
tellscolumn
to guess the number of columns based on the input-s...
specifies the delimiter character(s)
Result:
.gitignore FILE 224
Jenkinsfile FILE 1396
README.md FILE 237
This relies on the characters [
, ]
, ,
and "
not appearing in your filenames, which is not a safe assumption.
paste
can also arrange multiple inputs side-by-side. For this, we can remove the JSON structures altogether, and output raw lines (hat-tip to @muru):
jq -r '.children.values|.path.components[0],.type,.size' | paste - - -
paste - - -
means 3 columns, all read from the the same source. This time, the only assumption is that the filenames don't contain newlines.
add"
to-s
and you have the exact output that OP wants
– RoVo
17 mins ago
1
Or if entries won't have newlines in them:jq -r '.children.values | .path.components[0],.type,.size' | paste - - -
– muru
16 mins ago
@JigglyNaga thanks for the detailed answer but I have one more question assume if I want store size value in variable how would I do that , because I want to convert those size values to MB
– Sugatur Deekshith S N
10 mins ago
then you can use my answer ;-)
– RoVo
9 mins ago
add a comment |Â
up vote
0
down vote
For the use case provided in the Question, @JigglyNaga's answer is probably better than this, but for some more complicated task, you could also loop through the list items using keys
:
for k in $(jq '.children.values | keys | .' file); do
...
done
So e.g. you might use:
for k in $(jq '.children.values | keys | .' file); do
value=$(jq -r ".children.values[$k]" file);
name=$(echo "$value" | jq -r '.path.name');
type=$(echo "$value" | jq -r '.type');
size=$(echo "$value" | jq -r '.size');
printf '%st%st%sn' "$name" "$type" "$size";
done | column -t -s$'t'
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Extracting the members
jq -c '.children.values|[.path.components[0],.type,.size]'
.children.values
outputs every member of the array.values
.|
pipes the previous result through the next filter, rather like a shell pipe[
...,
...,
...]
makes all the terms inside appear in a single array- The
-c
option produces "compact" format ie. one object per line
Result:
[".gitignore","FILE",224]
["Jenkinsfile","FILE",1396]
["README.md","FILE",237]
...
Formatting the result
If you want to output a neatly-aligned table, that's a task better handled by other tools, such as column
or paste
.
jq -c '.children.values|[.path.components[0],.type,.size]' | column -t -s',"'
-t
tellscolumn
to guess the number of columns based on the input-s...
specifies the delimiter character(s)
Result:
.gitignore FILE 224
Jenkinsfile FILE 1396
README.md FILE 237
This relies on the characters [
, ]
, ,
and "
not appearing in your filenames, which is not a safe assumption.
paste
can also arrange multiple inputs side-by-side. For this, we can remove the JSON structures altogether, and output raw lines (hat-tip to @muru):
jq -r '.children.values|.path.components[0],.type,.size' | paste - - -
paste - - -
means 3 columns, all read from the the same source. This time, the only assumption is that the filenames don't contain newlines.
add"
to-s
and you have the exact output that OP wants
– RoVo
17 mins ago
1
Or if entries won't have newlines in them:jq -r '.children.values | .path.components[0],.type,.size' | paste - - -
– muru
16 mins ago
@JigglyNaga thanks for the detailed answer but I have one more question assume if I want store size value in variable how would I do that , because I want to convert those size values to MB
– Sugatur Deekshith S N
10 mins ago
then you can use my answer ;-)
– RoVo
9 mins ago
add a comment |Â
up vote
3
down vote
Extracting the members
jq -c '.children.values|[.path.components[0],.type,.size]'
.children.values
outputs every member of the array.values
.|
pipes the previous result through the next filter, rather like a shell pipe[
...,
...,
...]
makes all the terms inside appear in a single array- The
-c
option produces "compact" format ie. one object per line
Result:
[".gitignore","FILE",224]
["Jenkinsfile","FILE",1396]
["README.md","FILE",237]
...
Formatting the result
If you want to output a neatly-aligned table, that's a task better handled by other tools, such as column
or paste
.
jq -c '.children.values|[.path.components[0],.type,.size]' | column -t -s',"'
-t
tellscolumn
to guess the number of columns based on the input-s...
specifies the delimiter character(s)
Result:
.gitignore FILE 224
Jenkinsfile FILE 1396
README.md FILE 237
This relies on the characters [
, ]
, ,
and "
not appearing in your filenames, which is not a safe assumption.
paste
can also arrange multiple inputs side-by-side. For this, we can remove the JSON structures altogether, and output raw lines (hat-tip to @muru):
jq -r '.children.values|.path.components[0],.type,.size' | paste - - -
paste - - -
means 3 columns, all read from the the same source. This time, the only assumption is that the filenames don't contain newlines.
add"
to-s
and you have the exact output that OP wants
– RoVo
17 mins ago
1
Or if entries won't have newlines in them:jq -r '.children.values | .path.components[0],.type,.size' | paste - - -
– muru
16 mins ago
@JigglyNaga thanks for the detailed answer but I have one more question assume if I want store size value in variable how would I do that , because I want to convert those size values to MB
– Sugatur Deekshith S N
10 mins ago
then you can use my answer ;-)
– RoVo
9 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Extracting the members
jq -c '.children.values|[.path.components[0],.type,.size]'
.children.values
outputs every member of the array.values
.|
pipes the previous result through the next filter, rather like a shell pipe[
...,
...,
...]
makes all the terms inside appear in a single array- The
-c
option produces "compact" format ie. one object per line
Result:
[".gitignore","FILE",224]
["Jenkinsfile","FILE",1396]
["README.md","FILE",237]
...
Formatting the result
If you want to output a neatly-aligned table, that's a task better handled by other tools, such as column
or paste
.
jq -c '.children.values|[.path.components[0],.type,.size]' | column -t -s',"'
-t
tellscolumn
to guess the number of columns based on the input-s...
specifies the delimiter character(s)
Result:
.gitignore FILE 224
Jenkinsfile FILE 1396
README.md FILE 237
This relies on the characters [
, ]
, ,
and "
not appearing in your filenames, which is not a safe assumption.
paste
can also arrange multiple inputs side-by-side. For this, we can remove the JSON structures altogether, and output raw lines (hat-tip to @muru):
jq -r '.children.values|.path.components[0],.type,.size' | paste - - -
paste - - -
means 3 columns, all read from the the same source. This time, the only assumption is that the filenames don't contain newlines.
Extracting the members
jq -c '.children.values|[.path.components[0],.type,.size]'
.children.values
outputs every member of the array.values
.|
pipes the previous result through the next filter, rather like a shell pipe[
...,
...,
...]
makes all the terms inside appear in a single array- The
-c
option produces "compact" format ie. one object per line
Result:
[".gitignore","FILE",224]
["Jenkinsfile","FILE",1396]
["README.md","FILE",237]
...
Formatting the result
If you want to output a neatly-aligned table, that's a task better handled by other tools, such as column
or paste
.
jq -c '.children.values|[.path.components[0],.type,.size]' | column -t -s',"'
-t
tellscolumn
to guess the number of columns based on the input-s...
specifies the delimiter character(s)
Result:
.gitignore FILE 224
Jenkinsfile FILE 1396
README.md FILE 237
This relies on the characters [
, ]
, ,
and "
not appearing in your filenames, which is not a safe assumption.
paste
can also arrange multiple inputs side-by-side. For this, we can remove the JSON structures altogether, and output raw lines (hat-tip to @muru):
jq -r '.children.values|.path.components[0],.type,.size' | paste - - -
paste - - -
means 3 columns, all read from the the same source. This time, the only assumption is that the filenames don't contain newlines.
edited 2 mins ago
answered 28 mins ago


JigglyNaga
3,222626
3,222626
add"
to-s
and you have the exact output that OP wants
– RoVo
17 mins ago
1
Or if entries won't have newlines in them:jq -r '.children.values | .path.components[0],.type,.size' | paste - - -
– muru
16 mins ago
@JigglyNaga thanks for the detailed answer but I have one more question assume if I want store size value in variable how would I do that , because I want to convert those size values to MB
– Sugatur Deekshith S N
10 mins ago
then you can use my answer ;-)
– RoVo
9 mins ago
add a comment |Â
add"
to-s
and you have the exact output that OP wants
– RoVo
17 mins ago
1
Or if entries won't have newlines in them:jq -r '.children.values | .path.components[0],.type,.size' | paste - - -
– muru
16 mins ago
@JigglyNaga thanks for the detailed answer but I have one more question assume if I want store size value in variable how would I do that , because I want to convert those size values to MB
– Sugatur Deekshith S N
10 mins ago
then you can use my answer ;-)
– RoVo
9 mins ago
add
"
to -s
and you have the exact output that OP wants– RoVo
17 mins ago
add
"
to -s
and you have the exact output that OP wants– RoVo
17 mins ago
1
1
Or if entries won't have newlines in them:
jq -r '.children.values | .path.components[0],.type,.size' | paste - - -
– muru
16 mins ago
Or if entries won't have newlines in them:
jq -r '.children.values | .path.components[0],.type,.size' | paste - - -
– muru
16 mins ago
@JigglyNaga thanks for the detailed answer but I have one more question assume if I want store size value in variable how would I do that , because I want to convert those size values to MB
– Sugatur Deekshith S N
10 mins ago
@JigglyNaga thanks for the detailed answer but I have one more question assume if I want store size value in variable how would I do that , because I want to convert those size values to MB
– Sugatur Deekshith S N
10 mins ago
then you can use my answer ;-)
– RoVo
9 mins ago
then you can use my answer ;-)
– RoVo
9 mins ago
add a comment |Â
up vote
0
down vote
For the use case provided in the Question, @JigglyNaga's answer is probably better than this, but for some more complicated task, you could also loop through the list items using keys
:
for k in $(jq '.children.values | keys | .' file); do
...
done
So e.g. you might use:
for k in $(jq '.children.values | keys | .' file); do
value=$(jq -r ".children.values[$k]" file);
name=$(echo "$value" | jq -r '.path.name');
type=$(echo "$value" | jq -r '.type');
size=$(echo "$value" | jq -r '.size');
printf '%st%st%sn' "$name" "$type" "$size";
done | column -t -s$'t'
add a comment |Â
up vote
0
down vote
For the use case provided in the Question, @JigglyNaga's answer is probably better than this, but for some more complicated task, you could also loop through the list items using keys
:
for k in $(jq '.children.values | keys | .' file); do
...
done
So e.g. you might use:
for k in $(jq '.children.values | keys | .' file); do
value=$(jq -r ".children.values[$k]" file);
name=$(echo "$value" | jq -r '.path.name');
type=$(echo "$value" | jq -r '.type');
size=$(echo "$value" | jq -r '.size');
printf '%st%st%sn' "$name" "$type" "$size";
done | column -t -s$'t'
add a comment |Â
up vote
0
down vote
up vote
0
down vote
For the use case provided in the Question, @JigglyNaga's answer is probably better than this, but for some more complicated task, you could also loop through the list items using keys
:
for k in $(jq '.children.values | keys | .' file); do
...
done
So e.g. you might use:
for k in $(jq '.children.values | keys | .' file); do
value=$(jq -r ".children.values[$k]" file);
name=$(echo "$value" | jq -r '.path.name');
type=$(echo "$value" | jq -r '.type');
size=$(echo "$value" | jq -r '.size');
printf '%st%st%sn' "$name" "$type" "$size";
done | column -t -s$'t'
For the use case provided in the Question, @JigglyNaga's answer is probably better than this, but for some more complicated task, you could also loop through the list items using keys
:
for k in $(jq '.children.values | keys | .' file); do
...
done
So e.g. you might use:
for k in $(jq '.children.values | keys | .' file); do
value=$(jq -r ".children.values[$k]" file);
name=$(echo "$value" | jq -r '.path.name');
type=$(echo "$value" | jq -r '.type');
size=$(echo "$value" | jq -r '.size');
printf '%st%st%sn' "$name" "$type" "$size";
done | column -t -s$'t'
edited 10 mins ago
answered 15 mins ago
RoVo
2,132213
2,132213
add a comment |Â
add a comment |Â
Sugatur Deekshith S N is a new contributor. Be nice, and check out our Code of Conduct.
Sugatur Deekshith S N is a new contributor. Be nice, and check out our Code of Conduct.
Sugatur Deekshith S N is a new contributor. Be nice, and check out our Code of Conduct.
Sugatur Deekshith S N is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477210%2flooping-through-json-array-in-shell-script%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password