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":"success","result":{"stdout":"3.10.0-957.12.1.el7.x86_64\n","stderr":"","exit_code":0}} , {"node":"aaaa-cn002.me.com","status":"success","result":{"stdout":"3.10.0-957.10.1.el7.x86_64\n","stderr":"","exit_code":0}} , {"node":"aaaa-cn003.me.com","status":"success","result":{"stdout":"3.10.0-957.10.1.el7.x86_64\n","stderr":"","exit_code":0}} , {"node":"aaaa-cn004.me.com","status":"success","result":{"stdout":"3.10.0-957.12.1.el7.x86_64\n","stderr":"","exit_code":0}}

The desired output format is as follows:

aaaa-cn001.me.com 3.10.0-957.12.1.el7.x86_64
aaaa-cn002.me.com 3.10.0-957.10.1.el7.x86_64
aaaa-cn003.me.com 3.10.0-957.10.1.el7.x86_64
aaaa-cn004.me.com 3.10.0-957.12.1.el7.x86_64

Answer №1

Seems like you're looking for this solution:

jq --raw-output '.items[] | .node + " " + .result.stdout' out.json

Feel free to experiment with it here

Answer №2

If you're looking for a different approach, there's another method to achieve the same result - by utilizing a walk-path based Unix utility called jtc:

bash $ <out.json jtc -w'[items][:][node]<n>v[-1][result][stdout]' -T'"{n} {}"' -qq
aaaa-cn001.me.com 3.10.0-957.12.1.el7.x86_64

aaaa-cn002.me.com 3.10.0-957.10.1.el7.x86_64

aaaa-cn003.me.com 3.10.0-957.10.1.el7.x86_64

aaaa-cn004.me.com 3.10.0-957.12.1.el7.x86_64

bash $ 

It's worth noting that an extra line is present here due to the stdout values having trailing \n, which results in the additional space when unquoting the JSON string (-qq).

If you prefer to print without retaining that extra line, then use this format:

bash $ <out.json jtc -w'[items][:][node]<n>v[-1][result][stdout]:<(.*)\\n>R' -T'"{n} {$1}"' -qq
aaaa-cn001.me.com 3.10.0-957.12.1.el7.x86_64
aaaa-cn002.me.com 3.10.0-957.10.1.el7.x86_64
aaaa-cn003.me.com 3.10.0-957.10.1.el7.x86_64
aaaa-cn004.me.com 3.10.0-957.12.1.el7.x86_64
bash $ 

PS: Just to clarify, I am the developer behind jtc - a command-line tool for performing JSON operations.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Python Json.decoder.JSONDecodeError: Anticipating a ',' delimiter:

When attempting to load a dictionary into json, I encountered an error. strsdc = ''' {"ext":{"amznregion":["useast"],"someURL":"https://som_url.com/x/px/Iyntynt/{"c":"client&qu ...

Switching XML to JSON using React.js

Currently, I am in the process of developing an application with a requirement to retrieve data from a web API that provides XML responses. However, my objective is to obtain this information in JSON format, but unfortunately, the API does not offer suppor ...

Restricting the frequency at which a specific key value is allowed to appear in JSON documents

Within my JSON file, there is an array structured like this: { "commands": [ { "user": "Rusty", "user_id": "83738373", "command_name": "TestCommand", "command_reply": "TestReply" } ] } I have a requirement to restrict the num ...

NodeJS JSONStream causing memory exhaustion issue

I've encountered an issue while trying to stream a large JSON file (94mb in size) from an HTTP request to a local file using the JSONStream library in NodeJS. Despite setting a memory flag of 256mb with node --max-old-space-size=256 .\main.js, th ...

Setting up JSON.pm on a web server when shell access is restricted

My hosting provider (iPage) does not support JSON.pm installation. I prefer not to utilize the XML modules they have available for transferring data from a CGI script to a web page. Is there a way to work with JSON without requiring Perl installation on th ...

An error occurred in Python when attempting to add new values to an object, resulting in a "TypeError: ... is not JSON

Currently, I am dealing with a JSON object that looks like this: { "people":[ {"firstName":"Hasan Sait", "lastName":"Arslan", "email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99f1f8eaf8f7b7eaf8f0edb7f8ebeaf ...

Learn the process of converting C# code to Kotlin with the incorporation of a Json library

Hello, I am new to Kotlin and apologize for my poor English. I am trying to convert the code snippet below to Kotlin. However, I am having trouble finding the equivalent of [JsonExtensiondata] in Kotlin. public class ProofAttribute { [JsonProperty(&q ...

What is the best way to utilize multiple gmail accounts within a python project using the gmail API?

I am currently working on a project that involves integrating multiple Gmail accounts. I have successfully added the first Gmail account and it is functioning smoothly. However, I encountered an issue when trying to add additional accounts. Upon checking G ...

Tips for extracting the data from a JSON response using Swift 3

This JSON data is the server's response to my request, but I'm struggling to extract and parse the values correctly. {"items": [{"item": { "id":824, "company_id":31, "config_id":45, "imagesmall":null, "im ...

Deciphering the JSON array generated by PHP

When PHP returns a JSON array, the output may look like this: [ { "uid": "1", "username": "mike", "time_created": "2014-12-27 15:30:03", "time_updated": "2014-12-27 15:30:03", "time_expires": "2014-12-27 15:30:03" }, { "uid": ...

Deciphering Postman's JSON information

I'm having trouble parsing a JSON response from Postman. Below is the response body: { "teams": [ { "id": "MI6", "name": "James Bond's Workspace", ...

angular data binding returning the identifier instead of the content

I have been dealing with managed fields retrieved from a web server in the following format: { "fields":{ "relationshipStatus":[ { "fieldId":4, "name":"Committed" }, { "fieldId":2, ...

Guide to executing a Bulk JSON files data update through API calls in Talend

Our project requires handling bulk JSON files (from an SFTP server) with consistent metadata to enable Incremental data processing through APIs. The goal is to load this data into an Oracle DB, using a Date column as the primary key. If an incoming file ex ...

Retrieve data from the following fields: {"attending":0,"eventid":1,"userid":1} using PHP

After receiving an array from JAVA and using print_r($_POST), I have tried various methods to extract data but with no success. The format of the array is as follows: {"attending":0,"eventid":1,"userid":1} I've attempted different approaches found o ...

Tips for optimizing JSON parsing and database writing for faster performance

I have a task that involves parsing a 200MB json file and then writing the data into an sqlite3 database using Python. Currently, my code executes successfully but it takes about 9 minutes to complete the entire process. @transaction.atomic def create_dat ...

What process allows for this Twitter API response to be converted into HTML format automatically?

It is common knowledge that Twitter operates in an API-centric manner, meaning all Twitter apps retrieve their data through the API. Upon accessing (or .xml for those who prefer), a JSON formatted result with plaintext content and no additional formattin ...

Issues with JSON validation in Laravel 5.4 are causing problems

Having trouble validating JSON in my Laravel 5.4 POST request. The validator is failing even though the JSON appears to be valid. I suspect there may be an issue with my validation rules or implementation rather than a bug. The POST endpoint I have set up ...

Tips on adding additional information to a current JSON array using SwiftyJSON

I am currently working with an array of SwiftyJson data that has been declared and populated with relevant information. The code snippet used to populate the hoge array is as follows: self.hoge = JSON(data: data!) However, I now need to add new SwiftyJSON ...

What is the best way to send an Ajax Json Response to a php script?

My current setup involves an Ajax call using the xmlHttpRequest to retrieve data from a server. I aim to format this response into a specific string and share it on a different server. To achieve my goal, I am leveraging a php script for processing the fo ...

Simple steps to find a specific node within a Jobject

When dealing with unspecified JSON data, I have a loaded JObject. Now, I need to search for specific elements across the jObject and iterate through each one. For example: {Item : { PA : P , VA : { COLL: D} } } Or {Fields : { CA : P , MA : { COLL: Q} ...