Certainly! Here are more jq examples to help you understand its capabilities:
-
Selecting Multiple Fields:
To extract multiple fields from each object in an array:jq '.array[] | {field1: .field1, field2: .field2}' file.json -
Counting Elements:
To count the number of elements in an array:jq '.array | length' file.json -
Sorting an Array:
To sort an array of objects by a specific field:jq '.array | sort_by(.field)' file.json -
Updating Values:
To update a specific field in all objects:jq '.array[] |= (.field = "newValue")' file.json -
Combining Data:
To combine two arrays into one:jq '.array1 + .array2' file.json -
Conditional Logic:
To filter based on a condition:jq '.array[] | select(.field > 10)' file.json -
Extracting Nested Data:
To extract data from nested structures:jq '.parent.child[] | .field' file.json -
Creating a New JSON Structure:
To create a new JSON object from existing data:jq '{newKey: .oldKey, anotherKey: .anotherField}' file.json
These examples demonstrate various ways to manipulate and extract data from JSON using jq. If you have a specific JSON structure or task in mind, let me know, and I can provide more targeted examples!
