Can you show more jq examples?

0159

Certainly! Here are more jq examples to help you understand its capabilities:

  1. Selecting Multiple Fields: To extract multiple fields from each object in an array:

    jq '.array[] | {field1: .field1, field2: .field2}' file.json
    
  2. Counting Elements: To count the number of elements in an array:

    jq '.array | length' file.json
    
  3. Sorting an Array: To sort an array of objects by a specific field:

    jq '.array | sort_by(.field)' file.json
    
  4. Updating Values: To update a specific field in all objects:

    jq '.array[] |= (.field = "newValue")' file.json
    
  5. Combining Data: To combine two arrays into one:

    jq '.array1 + .array2' file.json
    
  6. Conditional Logic: To filter based on a condition:

    jq '.array[] | select(.field > 10)' file.json
    
  7. Extracting Nested Data: To extract data from nested structures:

    jq '.parent.child[] | .field' file.json
    
  8. 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!

0 Comments

no data
Be the first to share your comment!