Even better, you can also implement your own functions, if you want/need to. Most of these functions tend to mirror JavaScript functions and might feel familiar. Jq offers much more than simple filtering and has many powerful built-in functions that let you perform a variety of useful operations on JSON data. cat data.json | jq '.data.posts | map(.title)' Functions / Filters Using jq’s built-in map function, this can also be written in a more readable way. For further processing, the latter may be the more useful as it allows to use the transformed array. While both return the title field for each item, the first commands returns the values as a string list while the second command wraps the string list back to an array. cat data.json | jq '.'Ĭat data.json | jq '.title]' However, there’s an import different between the following commands. cat data.json | jq '.' cat data.json | jq '.data.posts' | jq '.title' The following commands are doing the exact same thing. Using a regular shell pipe ( |), you may want/need to chain jq calls which can be useful when needing to apply filters step by step, e.g. To select from the end, you can use negative ranges. This is especially useful when you need to return a sub-array of an array. Using the square brackets notation, you can also select ranges and slice arrays. title filter as seen before.Īs with all arrays, you can also directly access any of the elements in the array by passing in their index, allowing you to access the properties of an object in the array directly. In the last step, the title property of each object is extracted with the. data.posts and then, due to the pipe ( |), passes each object in the array to the next filter in the command. The command first iterates over the entire array with. In case the array is containing objects, you can extract properties from each object in the array by chaining commands. The following example shows how to iterate over an array. As with many programming languages, jq uses square brackets to indicate the beginning and end of an array. Now let's look at how you can work with arrays in JSON data. (If any of the properties contain spaces or special characters, you must enclose the property name in quotes like jq '."some property name"'.) Working with arrays If you need to retrieve multiple keys, you can separate them with a comma. JavaScript, allowing you to access nested objects. You can also chain property values as known from e.g. to find a property value and simply combine this filter with the respective property path. Now you can see and navigate through the formatted JSON output.īased on aforementioned identity filter, you can access property values by using another simple filter, the. cat data.json | jqĪctually, calling jq without any argument/path is a shortcut for the identity filter jq '.' which returns all its input. Passing any kind of JSON to jq without any additional configuration will prettify the JSON so it’s easier to read and understand. When working with data from APIs, these usually return minifed JSON (to reduce the transferred amount of data) as mentioned before. curl ) or as output of a preceding command accordingly. cat data.json) in the upcoming examples, the data could be returned e.g. All of the following commands and filter demos are based on the following JSON document input as data. First of all, stedolan/jq offers prebuilt binaries for nearly any platform, so download and install jq if you haven’t done so already.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |