如何解析与linux的shell脚本的JSON [英] How to parse json with shell scripting with linux
问题描述
我有一个JSON输出,我需要在Linux中提取少量的参数。
I have a Json output from which I need to extract a few parameters in linux.
这是JSON输出:
{
"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":[
{
"Primary":true,
"PrivateIpAddress":"10.120.134.248"
}
],
"Attachment":{
"Status":"attached",
"DeviceIndex":0,
"DeleteOnTermination":true,
"AttachmentId":"eni-attach-9210dee8",
"AttachTime":"2014-03-19T09:16:56.000Z"
},
"Groups":[
{
"GroupName":"Test",
"GroupId":"sg-123456cb"
}
],
"SubnetId":"subnet-31236514",
"OwnerId":"109030037527",
"PrivateIpAddress":"10.120.134.248"
}
],
"SourceDestCheck":true,
"Placement":{
"Tenancy":"default",
"GroupName":null,
"AvailabilityZone":"us-east-1c"
},
"Hypervisor":"xen",
"BlockDeviceMappings":[
{
"DeviceName":"/dev/sda",
"Ebs":{
"Status":"attached",
"DeleteOnTermination":false,
"VolumeId":"vol-37ff097b",
"AttachTime":"2014-03-19T09:17:00.000Z"
}
}
],
"Architecture":"x86_64",
"KernelId":"aki-88aa75e1",
"RootDeviceName":"/dev/sda1",
"VirtualizationType":"paravirtual",
"Tags":[
{
"Value":"Server for testing RDS feature in us-east-1c AZ",
"Key":"Description"
},
{
"Value":"RDS_Machine (us-east-1c)",
"Key":"Name"
},
{
"Value":"1234",
"Key":"Cost.centre"
},
{
"Value":"Jyoti Bhanot",
"Key":"Owner"
}
],
"AmiLaunchIndex":0
}
]
}
期望的输出:
Instance id Name cost centre Owner
i-1234576 RDS_Machine (us-east-1c) 1234 Jyoti Bhanot
我想写一个包含类似实例ID,标签如姓名,成本中心,业主标题的文件。下面从JSON输出,某些值。这里给出的输出仅仅是一个例子。
I want to write a file that contains headings like instance id, tag like name, cost centre, owner. Below that, certain values from the json output. The output here given is just a example.
我如何能做到使用sed和awk?
How can I do that using sed and awk?
任何铅是AP preciated。
Any lead is appreciated.
感谢
推荐答案
下面是一个使用 jsawk 。参考:解析与SED JSON和awk
Here's a sample using jsawk. Reference: Parsing json with sed and awk
设置:
首先下载 jsawk
https://开头的github .COM /米莎/ jsawk :
$ curl -L http://github.com/micha/jsawk/raw/master/jsawk > jsawk
$ chmod 755 jsawk && mv jsawk ~/bin/
您也许想安装 JS-DEVEL
第一才可以使用jsawk。我使用的Fedora,所以我所做的是:
You might want to install js-devel
first before you can use jsawk. I'm using Fedora, so what I did was:
$ sudo yum install js-devel
测试:
我复制你的JSON输出采样到一个文本文件中。把它叫做 sample.json
。在这里,一个样品从您的JSON输出采样得到的值:
I copied your JSON output sample to a text file. Called it sample.json
. Here a sample to get a value from your JSON output sample:
$ jsawk 'return this.Instances[0].Monitoring.State' < sample.json
disabled
$ jsawk 'return this.Instances[0].VpcId' < sample.json
vpc-86bab0e4
有关从URL JSON数据,你可以使用卷曲http://someserver.com/data.json
而不是猫$的C $ C>:
For JSON data from a URL, you can use curl http://someserver.com/data.json
instead of cat
:
$ curl http://someserver.com/data.json | jsawk 'return this.Instances[0].VpcId'
vpc-86bab0e4
您可以在庆典
文件中使用这些命令生成包含字符串/文本,你想一个新的文件。您可以从我这里提供的链接GitHub的了解更多关于 jsawk
。
You can use these commands in your bash
file to generate a new file that contains strings / text that you wanted. You can read more about jsawk
from the GitHub link that I provided here.
这是你要找的是什么?
这篇关于如何解析与linux的shell脚本的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!