Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using JQ to extract information from json file

I currently have a JSON file that is around 15K of lines of code. I wish to extract some info and can do it individually using the commands bellow into 4 separate CSV files. I can’t figure out if I can reduce this into a single command so that all the data goes neatly into a CSV.

This is what I have so far.

jq -r '.ServiceEngine[]?.data_vnics[]?.vlan_interfaces[]?.vnic_networks[].ip.ip_addr.addr' config_AviCluster.json > config_AviCluster.IP_address.csv
jq -r '.ServiceEngine[]?.data_vnics[]?.vlan_interfaces[].if_name' config_AviCluster.json > config_AviCluster.Interface_Name.csv
jq -r '.ServiceEngine[]?.data_vnics[]?.vlan_interfaces[]?.vnic_networks[].ip.mask' config_AviCluster.json > config_AviCluster.IP_Mask.csv
jq -r '.ServiceEngine[]?.data_vnics[]?.vlan_interfaces[].vlan_id' config_AviCluster.json > config_AviCluster.vlan_ID.csv

That is 4 separate commands and then I manually merge the CSV into a single file. Could this be improved? If so any advice and guidance would be great.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Kinds regards

>Solution :

In theory, the following should create a new JSON structure with new information. But need a sample dataset to accurately test it.

jq -r '.ServiceEngine[]?.data_vnics[]?.vlan_interfaces[]|{ip_addr: .vnic_networks[].ip.ip_addr.addr, netmask: .vnic_networks[].ip.mask, iface: .if_name, vlan: .vlan_id}' config_AviCluster.json

Output

{
    "ip_addr": "0.0.0.0",
    "netmask": "255.255.255.0",
    "iface": "eth0",
    "vlan_id": "1"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading