0

I have set a variable like this below-

domain= ("*.abc" "*.xyz" "*.123")

I want set the value of this variable in a json file like below-

"Items": [
            "*.abc",
            "*.xyz",
            "*.123",]

But, the problem is bash script is skipping quotation "" and taking only inside the quotation. Other than this, bash is also trying to take the value as command. I just want to set the value in Items array including commas, that's it.

I am using jq --arg e1 ${domain[@]} to set the domain variable to e1 environment variable.

And getting this below error -

jq: error: syntax error, unexpected '*', expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1: *.xyz.com
5
  • If you want to have a quote as part of the string, you have to actually write one into the string, for instance domain= ('"*.abc"' '"*.xyz"' '"*.123"'). Commented Jun 22, 2022 at 9:20
  • 1
    @user1934428: The space after = produces a syntax error. Commented Jun 22, 2022 at 9:38
  • Ah, of course. I was stupidly copying from the original program. Of course it must be domain=('"*.abc"' '"*.xyz"' '"*.123"'). I assume the space was not present in the original code of the OP as well, otherwise execution would not even have reached the jq statement. Commented Jun 22, 2022 at 10:07
  • Bash or "shell"? Please read the descriptions of all the tags you applied! Commented Jun 22, 2022 at 10:24
  • space isn't there in original code, I tried with this ' ' but it's not working for jq as jq will append it for array string in Items Commented Jun 22, 2022 at 10:31

3 Answers 3

2

--arg doesn't understand bash arrays (some shells don't have any arrays).

You can use --args instead which populates $ARGS.positional with a list of remaining arguments.

domain=("*.abc" "*.xyz" "*.123")
jq '.Items = $ARGS.positional' <<<'{"Items":[]}' --args "${domain[@]}"

Note that I removed the space after domain=. With the space, bash throws a syntax error.

Sign up to request clarification or add additional context in comments.

1 Comment

or even shorter: jq -n '.Items = $ARGS.positional' --args "${domain[@]}
1

You could turn the bash array into a string and separate the items by, say, a newline character, then import the string using --arg, and split it up again into a jq array using /:

jq -n --arg e1 "$(printf '%s\n' "${domain[@]}")" '{Items: ($e1 / "\n")}'
{
  "Items": [
    "*.abc",
    "*.xyz",
    "*.123"
  ]
}

Comments

0

You can use --args to pass the array to jq as arguments, preserving the list/array structure:

domain=('*.abc' '*.xyz' '*.123')
jq -n --args '.Items = $ARGS.positional' "${domain[@]}"

Gives:

{
  "Items": [
    "*.abc",
    "*.xyz",
    "*.123"
  ]
}

Note that all the quoting is for the shell, not for the JSON. jq adds the quotes in the JSON. Add > myfile to jq to redirect the output to a file.

You can also skip the array and write '*.123' and so on directly, as arguments to jq.

2 Comments

jq: error: syntax error, unexpected QQSTRING_START, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1: .Items =$ARGS.positional "${domain[@]}" jq: 1 compile error
The syntax is correct. You have quoting issues. The syntax in your error is different from my example. Make sure to leave spaces and quotes exactly as the example. "${domain[@]}" shouldn't be in the jq command string, it should be after it. The error also says Windows cmd shell? The example is for bash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.