Search code examples
arraysjsonbashshelljenkins-cli

How to read Json file(array values) and print values using shell script


I have a Jenkins Job that prints a list of elements either in Json or Text format,I am trying to setup a Jenkins job that would print the first element ,second element..... form a JSON or TEXT.And I wanted to assign it to a variable so i can retrieve any element using its number,
"ex:- array[4]"
I am looking to read the output and print these values in shell.

and I am trying to use different ways from online to get to ,using arrays ,using echo and yet looking for other way, any documents or any information would be helpful for me to go through.

the output on Jenkins in JSON looks like:-

[
"Danilo"
]
[
    "Kevin"
]

the output on Jenkins in TEXT looks like:-

Danilo
Kevin

Any information or a place would be helpful. Thank you!


Solution

  • I have used for loop by getting the count of the words.And the output I stored in a txt file not json.

    so i used

    c1=$(wc-w < file.txt)
    file.txt is the name of the file used to input these values from example Danilo,Kevin,and c1 gives number of word count which is 2 in this example

    Now I used for loop:

    for i in `seq 1 $c1`
    do
    export Name$i=$(sed -n "$i"p file.txt)
    done

    this way we will get the output as Name1=Danilo and Name2=Kevin as I am exporting the value each time in the loop.