Difference between revisions of "Jq"

From wikieduonline
Jump to navigation Jump to search
 
(19 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
* Homepage: https://stedolan.github.io/jq/
 
* Homepage: https://stedolan.github.io/jq/
 
* Ubuntu: <code>[[apt install jq]]</code>
 
* Ubuntu: <code>[[apt install jq]]</code>
 +
 +
[[jq -r]]
 +
[[jq -c]]
  
 
== Examples ==
 
== Examples ==
 +
 
  jq . yourfile.[[json]]
 
  jq . yourfile.[[json]]
  jq . -r yourfile.[[json]]
+
  jq -r . yourfile.[[json]]
 
  [[jq length]] yourfile.[[json]]
 
  [[jq length]] yourfile.[[json]]
 
  [[jq keys]] yourfile.[[json]]
 
  [[jq keys]] yourfile.[[json]]
  
jq ".YourReg.Yourfield" yourfile.[[json]]
+
<code>[[jq -r]] .YourReg.Yourfield  yourfile.[[json]]</code>
 +
 
 +
<code>jq -r '. | .YourReg[].Yourfield.yoursubfiel'</code>
  
jq '.[] | [[select]](.color=="blue")' your_json_file.json
+
<code>jq '.[] | [[select]](.color=="blue")' your_json_file.json</code>
  
 
To merge:
 
To merge:
jq -s '.[0] * .[1]' file1 file2
+
 
 +
<code>jq -s '.[0] * .[1]' file1 file2</code>
 +
 
 +
* <code>[[aws secretsmanager]] get-secret-value --secret-id test/pass | [[jq -r]] '.SecretString' | jq -r '.password'</code>
 +
 
 +
[[jq --help]]
 +
-r              output raw strings, not JSON texts;
 +
 
 +
* <code>[[jq]] '[[sort_by]](.your_field) | reverse'</code>
 +
 
 +
cat [[.terraform/terraform.tfstate]] | [[jq -c]] '.backend.config.bucket' | tr -d '"' | grep -c "$(echo $AWS_ENV)"
 +
 
 +
 
 +
cat file.txt | jq '.Quotas[] | select(.QuotaName | [[contains]]("G and VT")) | {QuotaName, Value, QuotaArn, QuotaCode, Unit}'
 +
 
 +
== Errors ==
 +
* <code>[[parse error]]: Invalid numeric literal at line 1, column 2</code>
  
 
== Related ==
 
== Related ==
Line 22: Line 44:
 
* [[SQL]]
 
* [[SQL]]
 
* <code>[[yq]]</code>
 
* <code>[[yq]]</code>
 +
* Miller: <code>[[mlr]]</code>
 
* <code>[[mapfile]] -t arr < <(jq -r 'keys[]' yourfile.json)</code>
 
* <code>[[mapfile]] -t arr < <(jq -r 'keys[]' yourfile.json)</code>
 +
* <code>[[Cannot iterate over null]]</code>
 +
* <code>[[Cannot index string with string]]</code>
  
 
== Activities ==
 
== Activities ==
Line 31: Line 56:
 
* {{JSON}}
 
* {{JSON}}
  
[[Category:Computing]]
+
[[Category:jq]]
 +
[[Category:JSON]]

Latest revision as of 14:21, 22 May 2024

jq is a lightweight and flexible command-line JSON processor.

jq -r
jq -c

Examples[edit]

jq . yourfile.json
jq -r . yourfile.json
jq length yourfile.json
jq keys yourfile.json

jq -r .YourReg.Yourfield yourfile.json

jq -r '. | .YourReg[].Yourfield.yoursubfiel'

jq '.[] | select(.color=="blue")' your_json_file.json

To merge:

jq -s '.[0] * .[1]' file1 file2

jq --help
-r               output raw strings, not JSON texts;
cat .terraform/terraform.tfstate | jq -c '.backend.config.bucket' | tr -d '"' | grep -c "$(echo $AWS_ENV)"


cat file.txt | jq '.Quotas[] | select(.QuotaName | contains("G and VT")) | {QuotaName, Value, QuotaArn, QuotaCode, Unit}'

Errors[edit]

  • parse error: Invalid numeric literal at line 1, column 2

Related[edit]

Activities[edit]

See also[edit]

Advertising: