Questions tagged [jq]

jq is a remarkable tool specifically designed for manipulating JSON data. It grants users unparalleled flexibility in slicing, filtering, mapping, and transforming structured information. Similar to the ease with which sed, awk, grep, and their companions facilitate text manipulation, jq provides equivalent simplicity and efficiency when working with JSON data.

Filtering values with spaces using JQ

I'm facing an issue where I need to select and filter a value that contains a space in it. My attempt involves using a for loop, but unfortunately, I'm not getting any output with this approach. I even tried utilizing --arg name "$i", however, th ...

Change the name of a series of JSON objects

Among my JSON entities, there is one called 'info'. Take a look at it: null null { "id": "qwefhu214o", "number": "2346", "date": "28.01.2019" } null null { "id": " ...

Retrieve lines from a document that fail to meet specific requirements

To tackle the issue, my goal is to extract lines from a file that do not contain the specific pattern: "ddd":[ The target is to filter out lines like "memo": { or "notice": "delivered on 17", and only retrieve lines containing "ddd":[, disregar ...

Adjust the JSON array format by adding new values to a particular key

I've been working on this code snippet: [ ["John Black",[ ["Lorem ipsum dolor sit amet.",27], ["Ut tempus lectus ut mi.",23] ]], ["Peter Pan",[ ["Quisque pulvinar odio.",22], ["Nec ut lorem quis inter ...

Tips for extracting values from an array that was fetched using cURL and stored as JSON with .JQ

{ "accounts": [ { "accountId": "account001", "tokens": [ "tokens1", "tokens2", "tokens3", "tokens4", ] } ] } the ...

Combining and organizing JSON arrays

Having an issue with jq. Received the following data from API: { "items": [4,5,1,3,2], "objectNumbers": [ { "type": "objectNumber", "number": 8 }, { "type": "objectNumber", "number": 7 }, { ...

Divide various text files into separate arrays using jq

I'm faced with a challenge involving files containing multiple key value pairs that I need to convert into arrays. Allow me to demonstrate this with some examples from the file contents: # cat content/1.yaml time: "2020-09-14T22:33:40Z" id: ...

Parsing a multi-dimensional JSON object in Bash (az cli, azure) can be achieved by separating the object IDs using colons and converting them into an array

Exploring with jq and jmespath: I am intrigued by the objectId located in the middle of this string... I've attempted to use sed, regex, but there has to be a more clever solution... Command 1: az dls fs access show --account "$account" --path "$rootpath ...

JSON: A guide on transforming a flat JSON structure into a nested one

I've been struggling to transform a rather flat JSON structure into one with more depth, but I haven't had any luck so far. Here is the initial data: [ { "id": "27", "time": "2017-12-21 07:24:00", "service_name": "prices", "vers ...

Obtain the package version from Ansible output by employing jq

I need assistance parsing the below JSON output from Ansible using jq. Specifically, I am trying to extract the version number for a package named GeoIP. How can I efficiently utilize jq to retrieve this version number for the specified package? The desir ...

embed the JSON data from one file into the vacant array within a different file's JSON structure

My goal is to include this block of code from the devices_format.json file: { "devices": [ { "name": "new_elsys", "objects": [ { "name": "ServerRoomTemp", "datapoint_type": "measurement", "coder_fiel ...

What is the best method for extracting a specific block of code from a json file with jq?

I have a json file called temp.json that looks like this: { "data": { "stuff": [ ..... ] }, "time": { "metrics": 83 } } I am looking to remove the following block of code from the JSON file: , ...

Choosing from various criteria in JQ when one is a partial match

In my search, I have found many responses that are similar to what I need, but not quite there. I am working on parsing a JSON file using the following command: cat file.json | jq -r ".[] | select(.name == \"${VALUE}\" or .guid == ...

How can I utilize jq to gracefully extract values from a nested JSON object that may contain nulls

I encountered a JSON response consisting of three items with the following structure: { "id": 44, "extra": [{ "domain": { "id": 3, "name": "person" ...

Utilizing jq for finding a parent element based on a specific child key/value pair

How can I use jq to select a parent object that contains a child object meeting two filter requirements? For instance, I want to choose all Subnets elements with a child tag having key "Name" and value "TheName". There are two subnets in my example - one ...

Switch out a key with an array in a bash script

Here is an example json file named payload.json.tpl: { "foo": "bar", "x": { "y": "${array}" } } In a bash script, I have defined an array like this: array=("one" "two" "three") I am looking for a way to use the jq command to update the key .x ...

Encountering problems with hyphen escaping in top-level keys using jq

I am attempting to utilize jq to parse the following JSON data: { "aaa-bbb-ccc": { "derp": blah } } Unfortunately, I am encountering this error message: cat myjson | jq -r .'aaa-bbb-ccc' jq: error: aaa/0 is not defined at <top-level ...

Is there a way to use jq to divide a JSON stream of objects into individual files according to the values of a specific object property?

Dealing with a hefty file (20GB+ compressed) named input.json, filled with a stream of JSON objects like so: { "timestamp": "12345", "name": "Some name", "type": "typea" } { "timestamp": "12345", "name": "Some name", "type": "typea ...

Modifying the value of a JSON field within a Dockerfile

I am working with a "config.json" file in my Dockerfile to adjust specific parameters. I need to generate a random string to replace the value of a field within this file. If we consider a file structure similar to the code provided below, with the field " ...

What's the best way to update keys and values using regex pattern matching?

Need some assistance with jq: Is there a way to find "name" fields in JSON starting with an underscore (ex: _RDS_PASSWORD) and remove the underscore, resulting in RDS_PASSWORD? How can I utilize jq to locate "name" fields beginning with an underscor ...

Sending variables with spaces to the curl --data parameter

When dealing with arguments that contain spaces, it's important to note that they may not pass correctly to the curl command. Additionally, quotes might not be properly transferred in the --data field. For example, if I simply echo the variable &apos ...

Execute jq with the curl/bash command

I want to create a script in bash that retrieves the final URL after redirection. For example, when google.com is redirected to http://www.google.com with a status of 301, I want to capture that redirect. Here's what I have attempted so far: json=&ap ...

Employ jq to organize keys within a JSON object based on a specific property

Is there a way to sort the keys of a JSON file in natural order while prioritizing keys listed in the 'required' section? The command below sorts keys in natural order: jq --sort-keys . /tmp/source.json > ./tmp/target.json { "Request": { ...

Use jq to denote the current position within a loop

I am faced with a situation where I have two separate log files named log.json and messages.json. The log file is structured as follows: {"msg": "Service starting up!"} {"msg": "Running a job!"} {"msg": "Error detected!"} The messages file has the fol ...

Utilizing ANSI color codes in combination with jq

Exploring the integration of ANSI color codes with jq. Here are some test cases: $ echo '{"a":"b","c":"d"}' | jq -r .c d # Expected output $ echo '{"a":"b","c":"33[31md33[0m&q ...

Creating additional JSON fields with jq in cases where the value does not match

Here is a sample JSON structure that I have: { "environment": [ { "name": "user1", "value": "app" }, { "name": "user2", "value" ...

When iterating over every element in an array with jq

If I run the following command: kbc get pods -o=json | jq -c I will receive output like this: {"apiVersion":"v1","items":[{"name":"a"},{"name":"b"},{"name":"c"}]} Now, how can I access and display the name of each element in the items array? Would some ...

manipulating JSON data with jq

My JSON objects each require a transformation where the node named "last" needs to incorporate the sum of lengths for each part. Here is an example of one such object: Sample Input JSON object { "serialnumber": "5690", "duplicate": true, "pa ...

There is an issue with the format of the fromdate parameter in the JSON query. It should match the following format: "%Y-%m-%dT%H:%

Below is a snippet of JSON output I'm dealing with, specifically the LastAccessedDate value. This date format is provided by AWS CLI command and unfortunately, I cannot control its structure. { "MyList": [ { "Name": &qu ...

jq filter to exclude certain values in select statement by matching against an array of values

Given this JSON input : { "hostname": "server1.domain.name\nserver2.domain.name\n*.gtld.net", "protocol": "TCP", "port": "8080\n8443\n9500-9510", "component": ...

Using JQ to structure logs in shell scripting

I've been looking to save some shell scripts into a file for collection by an agent like Fluentbit and sending them off to Cloudwatch and Datadog. I came across this example online that works effectively with the use of jq. __timestamp(){ date "+%Y ...

How to eliminate an element from numerous arrays using jQuery?

Is it possible to use jq to remove all instances of a specific name from arrays within the input data? For example, removing "Name1" from the following: { "Category1": [ { "name": "Name1", "desc": "Desc1" }, { "name": "Name ...

Retrieve keys only in a concise list using JQ

I am working with a Pipfile.lock JSON file that requires parsing using the jq tool. The format of the file is as follows: { //... "default": { "value1": { // numerous nested properties with values ...

Guide on converting JSON array values into CSV format with JQ

I have a JSON file that stores information about different application clients along with their respective features: { "client-A": [ "feature-x" ], "client-B": [ "feature-x", "feature-y" ], "client-C": [ ...

Adjust the initial letter of every word with JQ Capitalization

I am currently working with a large JSON file using JQ to filter out unnecessary elements. While I have successfully achieved this, I encountered an issue with certain values being all capitalized strings. Unfortunately, JQ does not provide a built-in func ...

Obtain numerous JSON pages using jq

When making an API call, I receive a JSON response that can be formatted nicely using curl "https://mywebsite.com/api/cars.json&page=1" | jq '.' Displayed below is a sample of the output. Since there are multiple pages of data (112 total entries wi ...

Extracting information from JSON files with jq when the key is a string of numbers

Recently, I've started using jq and ran into a problem when trying to parse my json data. The json is stored in a variable as shown below: temp='{ "1": { "my_name": "one" }, "2": { "my_name": "two" } }' My goal now is to extract the value of my_name for ...

Organizing array entries by key and key values with jq

Here is a sample json data: { "hits": [ { "country": "PT", "level": "H2", "id": "id1" }, { "country": "CZ", "level&quo ...

Using jq filter to selectively extract key-value pairs in bash

After executing the given command aws ec2 describe-tags --filter "Name=resource-id,Values=i-8dh7435490fjksfd" I received this JSON response { "Tags": [ { "ResourceType": "instance", "ResourceId": "i-8dh7435490fjksf ...

How can jq be used to compactly format specific fields?

Is jq the optimal solution for formatting arbitrary JSON? cat my.json | jq . is effective for pretty-printing JSON, but it can expand each field on separate lines. However, what if certain fields are repetitive, like a list of points? How can we format m ...

Utilize jq to extract data from json output

Thank you for your continued support. I am facing an issue with converting a JSON output into CSV format. While I have managed to generate some output, it does not align with the desired outcome. Curl Command Output { "results": [{ "name ...

Searching for JSON data based on a specific key value filter using the jq query language

I am seeking assistance with filtering and extracting keys from a JSON file that contains key-value pairs. Specifically, I need to filter based on the inner key value pair within each key. Any guidance on how to accomplish this would be greatly appreciated ...

Converting a JSON object into an array with JQ

Looking to transform a map of objects into an array using jq Input: { "fish-chips": { "likeDislikeRatio": ["80%", "20%"], "country": ["BRIT_FOOD","USA_FOOD"] }, "saus ...

Filtering arrays using jq at a nested level

I have noticed that all the demo/examples I've come across are filtering on the first level, but my requirement is to perform array filtering with jq on the second level: { "TheArray": [ { "F1": "V11", "F2": "V12", "F3": "V13" ...

Find the total count of a specific string within a JSON dataset by utilizing the jq tool

I am looking to find the count of occurrences of "1.2.3.46:8983_emo" in a specific JSON string using jq. This particular value can be found under myApp and myApp_shadow, so the expected count should be 2 My current jq filter is: .cluster.collections.myA ...

Extracting the initial data from a JSON stream with jq

What could be the reason behind first(select(.updated_at=="2019-06-03T16:36:53.194Z")) providing more than one result with the given input? {"_id":"a","updated_at":"2019-06-03T16:36:53.194Z"} {"_id":"b","updated_at":"2019-06-03T19:27:15.192Z"} {"_id":"c", ...

Remove a JSON entity if a specific value within the entity corresponds to a specified value (using jq)

One of the objectives is to remove an entire object from a JSON file that includes a specified key/value pair in a JQ script. For instance, any objects with /unwanted-path/ in the path should be eliminated: input.json: [ { "path": & ...

Adjust the JSON array dimensions using JQ for a neat and organized outcome

The JSON data provided in the link (jq play) needs to be converted into a CSV format as shown below: "SO302993",items1,item2,item3.1,item3.2,item3.3, item3.4,... "SO302994",items1,item2,item3.1,item3.2, , ,... "SO302995",items1,item2,item3.1, ...

Sort and extract different levels of keys from highly intricate nested JSON data

Issue I am faced with the task of extracting the id, name, and preview image from a list of YouTube videos. I am utilizing youtube-dl to obtain a json output, which I then analyze for the keys id, title, and the nested array thumbnails. Data Entry For an ...

Query: Employ regex to pick out property values

Here is the json Object I am working with: { "foo": { "name": "Name 1", "color": "green", "something_else": { "name" : "Name 2" } }, "bar": { "name": "Something else", "color": "red" ...

Performing a wildcard search to replace deep child values and merging them back into the original JSON using JQ

Can someone help me with a JSON manipulation issue similar to the one discussed in this post about Unix jq parsing wildcards? I want to change the value of "c": "text1" to "c": "newtext", while also merging the modif ...

Converting personal injury claims into configuration maps with the help of jq

Here is my input: { "apiVersion": "apps/v1", "kind": "Deployment", "spec": { "template": { "spec": { "containers": [ { "volum ...

Unexpected behavior when merging objects containing arrays in jq

Struggling to merge multiple objects using jq? If MY_OBJECTS contains JSON objects with a single key each, you can combine them into a single array like this: $ echo ${MY_OBJECTS} | jq -s '.' ... Want to further combine these objects so that al ...

Encountering a shell script parsing problem: Could it be an unexpected INVALID_CHARACTER due to Windows cmd shell quoting

What am I trying to accomplish? I have a JSON file called sonar-report.json. My goal is to iterate through the sonar-report.json in a shell script and extract values from the JSON file. To achieve this, I am using jq as my JSON parser (). Below is the cod ...

Converting JSON to CSV with flexible array lengths: Harnessing the power of jq

I have retrieved a JSON data with the given structure { "type": "conversation", "id": "1234", "created_at": 1425586662, "initial_message": { "type": "initial_message", "id": "567", "body": "<p>Testing</p> ...

Leverage jq to combine keys that share the same identifier

Imagine having two JSON files: 'b.json' and 'a.json'. [ { "id": 3, "foo": "cannot be replaced, id isn't in a.json, stay untouched", "baz": "do not touch3" }, { "id": 2, "foo": "should be replaced with &ap ...

What is the best way to display several values on a single line using `jq`?

After parsing through my json data, I've come across the following structure: { "Items": [ { "id": { "S": "c921e4eb-5958-424a-ae3a-b9cada0d9481" }, &quo ...

Converting object keys to arrays using jq

My goal is to transform a CSV file where the headers represent keys and the values in each column form a list. Here's an example of the CSV data: mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21 6 160 ...

A guide on utilizing jq to extract specific fields from a JSON string

Is there a way to extract specific fields from a JSON file using jq? On running 'jq '.node' out.json', the output contains the word 'null' This is the content of the file named out.json head out.json { "items": [ {"node":"aaaa-cn001.me.com","status":"s ...

What are the steps to configure a conditional jq transformation?

I am looking for a way to manipulate JSON data that may contain either one or two values. The structure of the JSON could look like this: {"form":{"textinput1":"aaa"},"params":{"context":""}} or {"form":{"textinput1":"aaa"},"params":{"context": "somethi ...

choose based on attributes within sub-array

I'm currently working on automating the process of updating AWS Lambda layers and the associated functions. To identify the functions using a specific layer, I am parsing the JSON output from the AWS CLI when listing all the functions in my AWS account. Wh ...

Manipulate Json in jq with Shell Script

I am currently working on a shell script that updates specific values within a JSON array, which is stored in a separate file. The basic logic behind this script involves setting an environment variable called URL, pinging that URL, and then modifying the ...

Is there a way to combine two strings using jq even if one of them is null?

I am currently working on parsing AWS instance metadata to combine two values into one string (a name and an id). The challenge I am facing is that the name field sometimes returns as null, causing issues with appending in jq. Is there a way to instruct j ...

What is the best way to apply a filter to an optional property value only when it is present?

I have JSON data like this: { "message": "hi" } However, the format could also be like this: { "message": { "action": "foo" } } My goal is to remove any records where message.action == "foo" I ...

Using the jq tool to randomly select strings from a list and replace values in a JSON structure

I have come across many questions that are similar but none specifically addressing the dynamic merging of 2 files. My goal is to dynamically modify the structure below: { "features": [ { "type": "Feature", "properties": { "name" ...

Utilizing the jq tool in Unix, convert complex JSON data with multiple levels into a CSV file

I have a complex JSON structure as shown below: { "id": "id123", "details": { "prod": "prod123", "etype": "type1" }, "accounts": [ { ...

Decoding a complex JSON data structure located within an array of items

Searching for data on Wind / Direction values has been challenging. An attempt I made using jq in a bash script: cat accuweather_raw | jq ".Wind[].Direction[] | [.Localized]" Unfortunately, I keep encountering the error "cannot index array with ...

`Calculate elements as long as the predicate remains true`

I've got a series of JSON objects that look like this: {"id":"da1234563","message":"brown fox jump over a red log"} {"id":"da1234564","message":"brown fox jump over a red log"} ...

Utilizing the sub() function in jq to dynamically modify JSON values based on specified conditions

I'm in need of modifying certain values within JSON data and integrating it into an existing shell script. My aim is to utilize jq for this task, specifically the "sub()" function to trim a portion of a string value. Executing the following command line: ...

Using JQ for parsing deeply nested arrays

Having trouble extracting data from a JSON file: { "operations": [ { "operationName": "GetValue", "batch_size": "2", "orders": [ { "clientId": "7836", "validation_time": { "place": "136", ...

How to utilize JQ to find specific key, value pairs within a list of objects sharing identical keys

Consider the JSON object provided below: { "company1": { "employees": [ { "name": "John", "title": "CEO" }, { ...

Q: How can I retrieve an array of arrays containing JSON object values from an array of objects?

When given a JSON input structured like this: [ { "k1": "o1k1", "k2": "o1k2", "k_opt1": "o1k_xxx" }, { "k1": "o2k1", "k2": "o2k2", ...

Transform JSON data into CSV format using jq

{ "Users": [ { "Attributes": [ { "Name": "sub", "Value": "1" }, { ...

Organizing JSON data fetched from a curl request by using jq's group_by and count functions to generate a sorted and aggregated

Seeking assistance with a jq script using only jq. Can someone help in creating a script to extract data from the following command output: `curl --silent "https://api.surfshark.com/v3/server/clusters" | jq` The objective is to display the numbe ...

Retrieving items from JSON using JQ in a complex nested file without specific identifiers

Trying to extract specific attributes from a generated JSON file obtained through Amazon CLI using JQ. The catch is that the top-level attributes are not named, making it a bit more challenging. [ [ { "State": "running", ...