![]() The first step would be to use the array/object iterator `.` to get to the objects inside. Before that can happen however, another step is needed since the post objects are inside of an array. ![]() If you wanted to get the user id of the posts, you could use the object identifier “.userId”. Another way to achieve the same thing is by using `cat`. Also notice that to read a json file, you can pass the path/filename as the argument after the filter expression. It’s clear that there are 2 objects in the array when we look at it, so this example would be more useful with larger data sets. Let’s say we wanted to see how many items are in `posts.json`, `jq` has a built in `length` available. Some other http tools, for instance httpie, pretty print JSON by default. `jq` pretty prints all output, so it can be used as a simple way to make viewing JSON output from `curl` nicer. "body": "est rerum tempore vitae sequi sint nihil" "body": "quia et suscipit suscipit recusandae consequuntur" To look at some usage examples, we’ll use the following content in a JSON file `posts.json`. `jq` can be used to change the shape of JSON data to satisfy a specific use case. There are also built in operators, functions, and conditionals for more advanced use cases. They can be combined and/or piped to each other. Filters are the foundation of `jq`, they take an input and produce an output. If you are not already familiar, “`jq` is a lightweight and flexible command-line JSON processor”. This post will demonstrate some tasks that can be done with `jq` that I recently used on the migration project. In some cases I needed to verify the format of the data, whether it was an array, what the key was, or how many items there were. I was looking for a quick way to get some basic information about the source data. ![]() The migration source was a JSON api containing the field values of nodes in the Drupal 7 site. Recently, while working on a migration from a Drupal 7 site to a Drupal 8 site, the command line tool `jq` came in quite handy. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |