1

I have a variable, X, in a bash script that has a value like asw|uduu|sssdd. How can I convert it to a JSON array using jq?

A second variable, Y, has a value like A:1|B:1|C:1. How can I convert it to a JSON object using jq?

Please help me solve these conversion tasks.

1 Answer 1

1
echo 'asw|uduu|sssdd' | jq -Rc 'split("|")'

produces: ["asw","uduu","sssdd"]

The "-c" option is inessential here - it just compacts the output.

echo 'A:1|B:1|C:1' |
 jq -Rc 'split("|") | map( split(":") | {(.[0]): .[1]} ) | add'

produces: {"A":"1","B":"1","C":"1"}

To understand how the last one works, run the leftmost part of the pipeline first, and then add successive parts.

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

Comments

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.