JSON manipulation with Jaq – Alternative to JQ

JSON manipulation with Jaq is an efficient way to work with JSON data. Jaq provides an intuitive approach to filtering, mapping, and transforming JSON structures similar to jq but with an emphasis on simplicity and discoverability.

Table of Contents

 

Installation Steps

Installing Jaq is straightforward and can be done with the following command:

npm install -g @01mf02/jaq

For Mac/Linux

brew install jaq

 

Features of Jaq

Jaq comes equipped with various features to manipulate JSON data:

  • Filtering and extracting data
  • Transforming JSON structures
  • Support for Prettify to make JSON readable
  • Functions such as Map(), Select(), fromjson and tojson

Examples

Prettify JSON

To make JSON more readable, use the following:

echo '{"name":"Alice","age":30}' | jaq '. | @pretty'

Accessing Fields

To access fields in a JSON object:

echo '{"name":"Alice","age":30}' | jaq '.name'

Accessing Values

To retrieve values from a JSON array:

echo '["apple", "banana", "cherry"]' | jaq '.[1]'

 

Filtering Data Example

echo '{"name":"John", "age":30, "city":"New York"}' | jaq '{name, city}'

Transforming Data Example

echo '[{"name":"John", "age":26}, {"name":"Jane", "age":28}]' | jaq '.[] | {personName: .name, personAge: .age}'

Extracting Elements Example

echo '["one", "two", "three", "four"]' | jaq '.[2]'

Complex Structure Manipulation Example

echo '{"people": [{"name": "Alice"}, {"name": "Bob"}]}' | jaq '.people | map({key: .name, value: "present"})'

Conditional Logic Example

echo '["a", 1, "b", 3]' | jaq '.[] | if type=="number" then .+1 else . end'

Working with Arrays Example

echo '["apple", "banana", "cherry"]' | jaq '.[] as $item | "\($item) is a fruit"'

Aggregating Data Example

echo '[1, 2, 3, 4]' | jaq 'add'

Reducing Data Example

echo '[{"price": 15}, {"price": 20}]' | jaq '[.[] | .price] | reduce .[] as $item (0; .+$item)'
 

Map Function

Use the map function to manipulate arrays:

echo '[{"name":"Alice"},{"name":"Bob"}]' | jaq '.[] | .name'

Select

To filter elements based on a specific condition:

echo '[{"name":"Alice","age":30},{"name":"Bob","age":25}]' | jaq '.[] | select(.age > 28)'

From JSON

Parse a JSON string into a JSON object:

echo '"{\"name\":\"Alice\",\"age\":30}"' | jaq 'fromjson'

To JSON

Convert a JSON object into a JSON string:

echo '{"name":"Alice","age":30}' | jaq 'tojson'

Walk

To recursively process and transform all elements in a JSON document:

echo '{"user":{"name":"Alice","details":{"age":30}}}' | jaq 'walk(if type == "object" then with_entries(.key |= "new_" + .) else . end)'

The <em>walk</em> operator takes a filter expression as an argument. In the provided filter expression:
<em>if type == "object"</em> is a condition that checks if the current element being traversed is an object.
<em>then with_entries(.key |= "new_" + .)</em> is the action to be performed if the condition is true. It uses the with_entries function to transform each key-value pair in the object by appending

Conclusion

In this tutorial, we’ve explored the installation and features of Jaq for JSON manipulation. We’ve provided examples of how to prettify JSON, access fields and values, use map functions, select filters, and convert between JSON strings and objects. With Jaq, handling JSON data becomes a more structured and efficient process.

References