skip to Main Content

I have the following environment variable that I have included in my azure-pipelines.yaml file:

  - name: myVariable
    value: '{"key1":"value1","key2":"value2"}'

The problem is the way that the pipeline is passing along this variable. When echoed, it looks like this: key1:value1 key2:value2, and since my application is expecting a JSON string, this is throwing JSONDecodeError("Extra data", s, end).

I tried formatting the variable differently with escape characters as follows:

{"key1":"value1","key2":"value2"}

which resulted in this echoed variable as I would expect:

{"key1":"value1","key2":"value2"}

but my application code threw this error JSONDecodeError: Expecting property name enclosed in double quotes. I added double quotes as follows:

"{"key1":"value1","key1":"value1"}"

but then received the following error: While parsing a quoted scalar, find unknown escape character.

It seems like the pipeline file doesn’t like the format of the variable. I’m wondering if there is an issue with the way that I have formatted the variable. I’d like for the variable to be passed the way I have described above, but if all else fails I could pass it in a more agreeable format and have my application code parse it. Would appreciate any thoughts, thanks.

2

Answers


  1. Did you try this? "{"key1":"value1","key2":"value2"}"

    Login or Signup to reply.
  2. Not sure how your application invokes the json. Check the variable doc:

    Variables are always strings. If you want to use typed values, then you should use parameters instead.

    you could use convertToJson expression, it takes a complex object and outputs it as JSON.

    parameters:
      - name: listOfValues
        type: object
        default:
          this_is:
            a_complex: object
            with:
              - one
              - two
    
    steps:
    - script: |
        echo "${MY_JSON}"
      env:
        MY_JSON: ${{ convertToJson(parameters.listOfValues) }}
    

    output:

    enter image description here

    If you use it for variable:

    variables:
      - name: myVariable
        value: '{"key1":"value1","key2":"value2"}'
    
    steps:
    - script: |
        echo "${newVar}" 
      env:
         newVar: ${{ convertToJson(variables.myVariable) }}
    

    Output:

    enter image description here

    You could check to use parameter or variable for definition, and invoke ${MY_JSON} in your application.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search