Skip to main content
Share Your Experience: Take the 2024 Developer Survey

New answers tagged

3 votes

Do I have a cleaner way to assign a parametrized json string to a bash variable?

You're currently creating a string that's a mixture of format (names of fields, structure chars like { and [, etc.) and data (arguments to the function). I'd separate the data from the format and use ...
Ed Morton's user avatar
  • 32.1k
11 votes

Do I have a cleaner way to assign a parametrized json string to a bash variable?

Using jq to create pieces of JSON for each sub-element of the top-level payload document and then putting everything together. This way, you ensure that jq has an opportunity to encode every string, ...
Kusalananda's user avatar
  • 337k
13 votes
Accepted

Do I have a cleaner way to assign a parametrized json string to a bash variable?

Yes, there is a way that's cleaner for many types of JSON documents (though not all types), a here document: local pet_to_add=$(cat - <<END_DOC { "id": $id_pet, "...
Sotto Voce's user avatar
  • 4,379
7 votes
Accepted

How to use jq to get a value from two possible paths?

.b // .a.b This would extract the value of the top-level b key, but if that value is missing, null, or false (i.e, if the value is false in a boolean context), use the b key under a. The above would ...
Kusalananda's user avatar
  • 337k
0 votes

How to print path and key values of JSON file

The following answer is missing the precise syntax for the round-tripping we'd all like to see. But it makes up for that deficit with one additional feature: it's easy to memorize! $ cat ...
Erik Ostermueller's user avatar
2 votes

Issue with double quotes inside double quotes in JSON

Your immediate issue is with quoting. The first single quote in "value": "'$1'" ends the single-quoted string that makes up the argument to the --data option of curl. This makes ...
Kusalananda's user avatar
  • 337k
3 votes
Accepted

Issue with double quotes inside double quotes in JSON

Generally, to properly quote a variable within single quotes in Bash, use '"$var"' (single quote followed by double quote). The first single quote ends the single-quoted string, then there's ...
dbran's user avatar
  • 116
5 votes
Accepted

sort all arrays in a JSON file depth-first to get a consistent order

The walk() function seems perfect for this use case. It recursively applies the given filter to every JSON element, bottom up, and returns the result. As a matter of fact, sorting all arrays is one of ...
David Z's user avatar
  • 997

Top 50 recent answers are included