Home > Blockchain >  How to create json file with parameter varible in jenkins?
How to create json file with parameter varible in jenkins?

Time:12-01

I am creating json file using echo statement ,it works fine as long as static string.

echo '
{
    "common": {
        "baseUri": "https://mycompany.com",
        "ipPrefix": "192.23.4.",
     }
}' > test.json

But when I need to interpolate string inside echo command it is not working.

 echo '
    {
        "common": {
            "baseUri": "$company_name",
            "ipPrefix": "192.23.4.",
         }
    }' > test.json

How to create json file in jenkins with parameter values substituted?

CodePudding user response:

The issue is due to the quotes as suggested in the comments.

Script below should work for you:

company_name="https://mycompany.com"
echo '{
     "common": {
          "baseUri": "'$company_name'",
          "ipPrefix": "192.23.4."
     }
}' > test.json

Output:

{
     "common": {
          "baseUri": "https://mycompany.com",
          "ipPrefix": "192.23.4."
     }
}

The value of any variable can’t be read by single quote ', since it only represents the literal value of all characters within it. To get the value of company_name we need to end the single quotes, then add the variable and start single quotes again.

Parameter expansion
Quoting

CodePudding user response:

With pipeline utility steps plugin, you can write any groovy map straight to a JSON file:

def company_name = "http://mycompany.com/"
def map = [ 
    "common" : [ 
        "baseUri": company_name,
        "ipPrefix": "192.23.4."
    ]
]

writeJSON json: map, file: test.json
  • Related