08-15-2019
The trick of course if to create an array of JSON objects which are comma separated key, value pairs.
I don't think a tool like
jq is designed to "
think for you, as to how to create the array of JSON objects from raw data".
That's up to you to design, specific to this single application first.
Personally, I think you are jumping to tools like
jq too quickly before you fully understand / design your algorithm to take your raw output and create an array of JSON objects.
If it was me, I would write this in PHP and it would only take a few lines of PHP code to take your input and create the desired JSON file.
Either way, bash or PHP or Javascript, it a simply matter:
- Taking the output of the command as text as input into your code.
- Parsing this data.
- Creating a JSON object from each line of parsed data (the values), keeping the header line as the key to the JSON objects..
- Add the JSON object above to an another array.
- Repeat for the next line of data in your output
I have done this 100s of time (using PHP) and there is generally no short-cut to writing your algorithm to take raw text as data and parsing it into a JSON object and then creating an array of JSON objects.
However, generally speaking I create an array of arrays and then convert the final array to JSON; but it really depends on the format of the final JSON object and if it needs to be
stringified to be transmitted over the net.
Also, I would caution you not to try to create "one parser to rule them all" as the output of other commands will like need a different parser.
Then when you have done this for a number of different commands, you can consider if you want to build "the mother of all UNIX commands to JSON parser".
Reference:
PHP: json_encode - Manual
This User Gave Thanks to Neo For This Post:
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hello team,
I am running below command which is giving following output.
bash-3.00$ ps -eo pid,pcpu,args | sort +1n | grep -i java
12 0.0 grep -i java
8804 0.0 /opt/app/ccr/home/ccr/WebSphere/AppServer/java/bin/sparcv9/java -XX:+UnlockDiag
9241 0.0... (7 Replies)
Discussion started by: coolguyamy
7 Replies
2. Shell Programming and Scripting
Hi All,
I have a output like below values
val1=test.com
val2=10.26.208.11
val3=en1
val4=test-priv1.com
val5=192.168.3.4
val6=en2
val7=test-priv2.com
val8=192.168.4.4
val9=en3
val10=test-vip.com
val11=10.26.208.9
val12=$val3
I want to convet this output values into below... (1 Reply)
Discussion started by: kamauv234
1 Replies
3. Shell Programming and Scripting
Friends,
I am trying to convert my local server timezone EST to UTC and for which I used the TZ command, see below
$ date
Thu Dec 6 10:14:14 EST 2012
$
$ TZ=UTC date -d '10:14 EST'
Thu Dec 6 15:14:00 UTC 2012
Now I would like to have the same output in 'yyyymmdd hh:mm' format.
... (4 Replies)
Discussion started by: vivek_damodaran
4 Replies
4. Shell Programming and Scripting
Hello Experts,
Below is the record i have:
sample data attached
I want this record of each row to be in single line and there are multiple rowise unixtime mentioned e.g 11996327 , This needs to be converted to Human readdable data and time from multiple rows
Can you help me , it will be... (10 Replies)
Discussion started by: manishK
10 Replies
5. Shell Programming and Scripting
Hello everyone,
I have created a workflow that will pull down data via a RESTful API in JSON, then my code parses and modifies said data. The problem I have is that these APIs I am working with only accept XML to PUT/POST data, and I am looking for an easy way to convert my JSON file to XML.
... (2 Replies)
Discussion started by: Zaphod_B
2 Replies
6. Shell Programming and Scripting
Hello Everyone,
I have a sample file raw.txt as shown below :
Drive Bays
Bay Name : SD-2C
Number of Standby Power Supplies : 4
Number of Drive Enclosures : 12
Summary Status of Contained Modules
All... (6 Replies)
Discussion started by: rahul2662
6 Replies
7. Shell Programming and Scripting
Dear friends,
I'm getting below API result and i would like to format them with Shell scripting.
Input
"id": 9,
"description": "short desc",
"name": "test",
"name_with_namespace": "ABCD-PDFF-PLATFORM-TEST-V1 / test",
"path": "test",
"path_with_namespace":... (7 Replies)
Discussion started by: baluchen
7 Replies
8. UNIX for Beginners Questions & Answers
Hi All,
i am trying to print the solaris 11 packages in below required format, But i am unable to do that.
Current ouput :
root@abc# pkginfo -l | egrep '(BASEDIR|NAME|VERSION)' | awk '{print}'
NAME: QLogic 570x/571x Gigabit Ethernet Driver
VERSION: 11.11,REV=2009.11.11
... (7 Replies)
Discussion started by: balu1234
7 Replies
9. UNIX for Beginners Questions & Answers
This is the sample json I have pasted here. I want all the IP address strings to be converted into an array. For example "10.38.32.202" has to be converted to everywhere in the JSON. There are multiple IPs in a JSON I am pasting one sample object from the JSON. But the IPs already in an Array... (11 Replies)
Discussion started by: vinshas1
11 Replies
10. UNIX for Beginners Questions & Answers
Hi All,
i am trying to print the df -h ouput in json format. using below script.
#!/usr/bin/env bash
df -h > /tmp/sdf
nawk '{print " "$1" "$2" "$3" "$4" "$5" "$6" "}' /tmp/sdf > /tmp/sdf1
nawk 'NR==1 { for (i=1; i<=NF; i++) { f = i }}{ print $(f), $(f), $(f), $(f), ... (2 Replies)
Discussion started by: sravani25
2 Replies
LEARN ABOUT DEBIAN
json::rpc::client
JSON::RPC::Client(3pm) User Contributed Perl Documentation JSON::RPC::Client(3pm)
NAME
JSON::RPC::Client - Perl implementation of JSON-RPC client
SYNOPSIS
use JSON::RPC::Client;
my $client = new JSON::RPC::Client;
my $url = 'http://www.example.com/jsonrpc/API';
my $callobj = {
method => 'sum',
params => [ 17, 25 ], # ex.) params => { a => 20, b => 10 } for JSON-RPC v1.1
};
my $res = $client->call($uri, $callobj);
if($res) {
if ($res->is_error) {
print "Error : ", $res->error_message;
}
else {
print $res->result;
}
}
else {
print $client->status_line;
}
# Easy access
$client->prepare($uri, ['sum', 'echo']);
print $client->sum(10, 23);
DESCRIPTION
This is JSON-RPC Client. See <http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html>.
Gets a perl object and convert to a JSON request data.
Sends the request to a server.
Gets a response returned by the server.
Converts the JSON response data to the perl object.
JSON
::RPC::Client
METHODS
$client = JSON::RPC::Client->new
Creates new JSON::RPC::Client object.
$response = $client->call($uri, $procedure_object)
Calls to $uri with $procedure_object. The request method is usually "POST". If $uri has query string, method is "GET".
About 'GET' method, see to <http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html#GetProcedureCall>.
Return value is "JSON::RPC::ReturnObject".
$client->prepare($uri, $arrayref_of_procedure)
Allow to call methods in contents of $arrayref_of_procedure. Then you can call the prepared methods with an array reference or a list.
The return value is a result part of JSON::RPC::ReturnObject.
$client->prepare($uri, ['sum', 'echo']);
$res = $client->echo('foobar'); # $res is 'foobar'.
$res = $client->sum(10, 20); # sum up
$res = $client->sum( [10, 20] ); # same as above
If you call a method which is not prepared, it will "croak".
Currently, can't call any method names as same as built-in methods.
version
Sets the JSON-RPC protocol version. 1.1 by default.
id Sets a request identifier. In JSON-RPC 1.1, it is optoinal.
If you set "version" 1.0 and don't set id, the module sets 'JSON::RPC::Client' to it.
ua Setter/getter to LWP::UserAgent object.
json
Setter/getter to the JSON coder object. Default is JSON, likes this:
$self->json( JSON->new->allow_nonref->utf8 );
$json = $self->json;
This object serializes/deserializes JSON data. By default, returned JSON data assumes UTF-8 encoded.
status_line
Returns status code; After "call" a remote procedure, the status code is set.
create_json_coder
(Class method) Returns a JSON de/encoder in "new". You can override it to use your favorite JSON de/encoder.
JSON
::RPC::ReturnObject
"call" method or the methods set by "prepared" returns this object. (The returned JSON data is decoded by the JSON coder object which was
passed by the client object.)
METHODS
is_success
If the call is successful, returns a true, otherwise a false.
is_error
If the call is not successful, returns a true, otherwise a false.
error_message
If the response contains an error message, returns it.
result
Returns the result part of a data structure returned by the called server.
content
Returns the whole data structure returned by the called server.
jsontext
Returns the row JSON data.
version
Returns the version of this response data.
JSON
::RPC::ServiceObject
RESERVED PROCEDURE
When a client call a procedure (method) name 'system.foobar', JSON::RPC::Server look up MyApp::system::foobar.
<http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html#ProcedureCall>
<http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html#ServiceDescription>
There is JSON::RPC::Server::system::describe for default response of 'system.describe'.
SEE ALSO
<http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html>
<http://json-rpc.org/wiki/specification>
AUTHOR
Makamaka Hannyaharamitu, <makamaka[at]cpan.org>
COPYRIGHT AND LICENSE
Copyright 2007-2008 by Makamaka Hannyaharamitu
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
perl v5.10.1 2008-09-01 JSON::RPC::Client(3pm)