Difference between revisions of "Json queries with JMEPath and jq"
Jump to navigation
Jump to search
(Created page with "= Interactive tools = == Jiq == Install <code>jq</code> Install Golang <1.7 If your environmental variable <source> echo $GOPATH GOPATH=/home/ubuntu/.local/go </source> i...") |
|||
Line 11: | Line 11: | ||
to <code>.bashrc</code> or <code>.profile</code> files. | to <code>.bashrc</code> or <code>.profile</code> files. | ||
== jmespath.terminal == | == jmespath.terminal == | ||
== jq == | |||
=== Get a filtered list of instance name, type, virt type, instance id === | |||
At the beginning my simple convention for Json is <tt>[]</tt> is an array and <tt>{}</tt> is an object. | |||
Get an JSON output of all instances in your AWS accounty | |||
aws ec2 describe-instances --output json | jiq #pipe into jiq | |||
''.Reservations[].Instances[]'' returns all data that we need, therefore we pipe into new object { _key1: XXX, _key2: YYY }. Actually, the keys fields we define now, as: _name, _id, _id, _type. | |||
<source> | |||
Filter]> .Reservations[].Instances[] | {_name: .Tags[], _id: .InstanceId, _type: .InstanceType, _virtType: .Hypervisor } | |||
</source> | |||
Next, let's select only Tags we are interested in, from a list(object). Therefore, we pipe Tags via ''select'' function. Where the ''Tag'' name is ''Name'' and we want get value | |||
{name: .Tags[] | select(.Key=="Name").Value | |||
Full filter command: | |||
<source> | |||
[Filter]> .Reservations[].Instances[] | {name: .Tags[] | select(.Key=="Name").Value, id: .InstanceId, type: .InstanceType, virt_type: .Hypervisor } | |||
</source> | |||
== Awscli queries == | |||
<source> | |||
$ aws ec2 describe-instances \ | |||
--output table \ | |||
--query 'Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value,InstanceId,InstanceType]' | |||
And we get a nicely formatted table: | |||
----------------------------------------------- | |||
| DescribeInstances | | |||
+----------------+--------------+-------------+ | |||
| xxxxxxxxxx | i-a0169xxx | r3.large | | |||
| yyyyyyyyyy | i-11a46xxx | m3.large | | |||
| zzzzzzzzzzzzzz| i-07c4axxx | t2.medium | | |||
+----------------+--------------+-------------+ | |||
</source> |
Revision as of 18:09, 2 June 2018
Interactive tools
Jiq
Install jq
Install Golang <1.7
If your environmental variable
echo $GOPATH GOPATH=/home/ubuntu/.local/go
is set, so add
export PATH="$PATH:$HOME/.local/go"
to .bashrc
or .profile
files.
jmespath.terminal
jq
Get a filtered list of instance name, type, virt type, instance id
At the beginning my simple convention for Json is [] is an array and {} is an object.
Get an JSON output of all instances in your AWS accounty
aws ec2 describe-instances --output json | jiq #pipe into jiq
.Reservations[].Instances[] returns all data that we need, therefore we pipe into new object { _key1: XXX, _key2: YYY }. Actually, the keys fields we define now, as: _name, _id, _id, _type.
Filter]> .Reservations[].Instances[] | {_name: .Tags[], _id: .InstanceId, _type: .InstanceType, _virtType: .Hypervisor }
Next, let's select only Tags we are interested in, from a list(object). Therefore, we pipe Tags via select function. Where the Tag name is Name and we want get value {name: .Tags[] | select(.Key=="Name").Value
Full filter command:
[Filter]> .Reservations[].Instances[] | {name: .Tags[] | select(.Key=="Name").Value, id: .InstanceId, type: .InstanceType, virt_type: .Hypervisor }
Awscli queries
$ aws ec2 describe-instances \ --output table \ --query 'Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value,InstanceId,InstanceType]' And we get a nicely formatted table: ----------------------------------------------- | DescribeInstances | +----------------+--------------+-------------+ | xxxxxxxxxx | i-a0169xxx | r3.large | | yyyyyyyyyy | i-11a46xxx | m3.large | | zzzzzzzzzzzzzz| i-07c4axxx | t2.medium | +----------------+--------------+-------------+