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.jsonCounting Elements: To count the number of elements in an array:
jq '.array | length' file.jsonSorting an Array: To sort an array of objects by a specific field:
jq '.array | sort_by(.field)' file.jsonUpdating Values: To update a specific field in all objects:
jq '.array[] |= (.field = "newValue")' file.jsonCombining Data: To combine two arrays into one:
jq '.array1 + .array2' file.jsonConditional Logic: To filter based on a condition:
jq '.array[] | select(.field > 10)' file.jsonExtracting Nested Data: To extract data from nested structures:
jq '.parent.child[] | .field' file.jsonCreating 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!
