Unix & Linux: How to parse JSON with shell scripting in Linux?
The Question: I have a JSON output from which I need to extract a few parameters in Linux.
This is the JSON output:
{
"OwnerId": "121456789127",
"ReservationId": "r-48465168",
"Groups": [],
"Instances": [
{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": null,
"RootDeviceType": "ebs",
"State": {
"Code": 16,
"Name": "running"
},
"EbsOptimized": false,
"LaunchTime": "2014-03-19T09:16:56.000Z",
"PrivateIpAddress": "10.250.171.248",
"ProductCodes": [
{
"ProductCodeId": "aacglxeowvn5hy8sznltowyqe",
"ProductCodeType": "marketplace"
}
],
"VpcId": "vpc-86bab0e4",
"StateTransitionReason": null,
"InstanceId": "i-1234576",
"ImageId": "ami-b7f6c5de",
"PrivateDnsName": "ip-10-120-134-248.ec2.internal",
"KeyName": "Test_Virginia",
"SecurityGroups": [
{
"GroupName": "Test",
"GroupId": "sg-12345b"
}
],
"ClientToken": "VYeFw1395220615808",
"SubnetId": "subnet-12345314",
"InstanceType": "t1.micro",
"NetworkInterfaces": [
{
"Status": "in-use",
"SourceDestCheck": true,
"VpcId": "vpc-123456e4",
"Description": "Primary network interface",
"NetworkInterfaceId": "eni-3619f31d",
"PrivateIpAddresses": [
{
... (Please watch the video for the full question)
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 72 people ==
The availability of parsers in nearly every programming language is one of the
advantages of JSON as a data-interchange format.
Rather than trying to implement a JSON parser, you are likely better off using
either a tool built for JSON parsing such as http://stedolan.github.io/jq/ or a
general purpose script language that has a JSON library.
For example, using jq, you could pull out the ImageID from the first item of
the Instances array as follows:
jq '.Instances[0].ImageId' test.json
Alternatively, to get the same information using Ruby's JSON library:
ruby -rjson -e 'j = JSON.parse(File.read("test.json")); puts j["Instances"][0]
["ImageId"]'
I won't answer all of your revised questions and comments but the following is
hopefully enough to get you started.
Suppose that you had a Ruby script that could read a from STDIN and output the
second line in your example output[0]. That script might look something like:
#!/usr/bin/env ruby
require 'json'
data = JSON.parse(ARGF.read)
instance_id = data["Instances"][0]["InstanceId"]
name = data["Instances"][0]["Tags"].find {|t| t["Key"] == "Name" }["Value"]
With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free... | Images: https://stocksnap.io/license & others | With thanks to user user3086014 (https://unix.stackexchange.com/users/..., user Steven D (https://unix.stackexchange.com/users/..., user Skippy le Grand Gourou (https://unix.stackexchange.com/users/..., user Robert Jonczy (https://unix.stackexchange.com/users/..., user Nadrieril (https://unix.stackexchange.com/users/..., user mmmizani (https://unix.stackexchange.com/users/..., user Mick Giles (https://unix.stackexchange.com/users/..., user lgeorget (https://unix.stackexchange.com/users/..., user kenorb (https://unix.stackexchange.com/users/..., user Gilles 'SO- stop being evil' (https://unix.stackexchange.com/users/..., user Dmitry L. (https://unix.stackexchange.com/users/..., and the Stack Exchange Network (http://unix.stackexchange.com/questio.... Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.
Информация по комментариям в разработке