0

I have below json i want to take out all the macaddress list in one jason array how do i do ?

{
  "facts_hash": {
    "lib": "/usr/share/fdi/facts:/opt/extension/facts",
    "interfaces": "eno1,eno2,eno3,eno4,eno5,eno6,ens3f0,ens3f1,lo",
    "ipaddress_eno1": "192.168.101.135",
    "macaddress_eno1": "08:f1:ea:6d:03:3c",
    "netmask_eno1": "255.255.255.0",
    "mtu_eno1": "1500",
    "macaddress_eno2": "08:f1:ea:6d:03:3d",
    "mtu_eno2": "1500",
    "macaddress_eno3": "08:f1:ea:6d:03:3e",
    "mtu_eno3": "1500",
    "macaddress_eno4": "08:f1:ea:6d:03:3f",
    "mtu_eno4": "1500",
    "macaddress_eno5": "b8:83:03:81:4b:20",
    "mtu_eno5": "1500",
    "macaddress_eno6": "b8:83:03:81:4b:21",
    "mtu_eno6": "1500",
    "macaddress_ens3f0": "b8:83:03:84:d5:1c",
    "mtu_ens3f0": "1500",
    "macaddress_ens3f1": "b8:83:03:84:d5:1d",

  },
  "name": "daisy-joni-selitto-quinto",
  "id": 269
}
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
Bharat Gupta
  • 127
  • 1
  • 10

3 Answers3

1

You can use Object.keys to iterate key in object, startsWith to check macadress text and use reduce to output your requirement as

var result = Object.keys(obj.facts_hash).reduce((acc, item)=>{
  if(item.startsWith("macaddress")) console.log(obj.facts_hash[item]);
}, []);

or

  var result = Object.keys(obj.facts_hash).reduce((acc, item) => {
                if (item.startsWith("macaddress")) {
                    //console.log(obj.facts_hash[item]);
                    acc.push(obj.facts_hash[item]);
                }
                return acc;
            }, []);
  console.log(result);

var obj = {
  "facts_hash": {
    "lib": "/usr/share/fdi/facts:/opt/extension/facts",
    "interfaces": "eno1,eno2,eno3,eno4,eno5,eno6,ens3f0,ens3f1,lo",
    "ipaddress_eno1": "192.168.101.135",
    "macaddress_eno1": "08:f1:ea:6d:03:3c",
    "netmask_eno1": "255.255.255.0",
    "mtu_eno1": "1500",
    "macaddress_eno2": "08:f1:ea:6d:03:3d",
    "mtu_eno2": "1500",
    "macaddress_eno3": "08:f1:ea:6d:03:3e",
    "mtu_eno3": "1500",
    "macaddress_eno4": "08:f1:ea:6d:03:3f",
    "mtu_eno4": "1500",
    "macaddress_eno5": "b8:83:03:81:4b:20",
    "mtu_eno5": "1500",
    "macaddress_eno6": "b8:83:03:81:4b:21",
    "mtu_eno6": "1500",
    "macaddress_ens3f0": "b8:83:03:84:d5:1c",
    "mtu_ens3f0": "1500",
    "macaddress_ens3f1": "b8:83:03:84:d5:1d",

  },
  "name": "daisy-joni-selitto-quinto",
  "id": 269
}


var result = Object.keys(obj.facts_hash).reduce((acc, item)=>{
  if(item.startsWith("macaddress")) console.log(obj.facts_hash[item]);
}, []);

//console.log(result);
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
0

If you can use jq, this expression

 [.facts_hash[]|  to_entries[] | select(.key|startswith("mac")).value]

should output this array:

[
  "08:f1:ea:6d:03:3c",
  "b8:83:03:81:4b:21",
  "b8:83:03:84:d5:1c",
  "b8:83:03:84:d5:1d"
]

Another option would be to convert the json to xml and use xpath on it.

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • Thanks Jack for your reply, i tried the same on https://jqplay.org/ but getting error – Bharat Gupta May 08 '20 at 05:42
  • @BharatGupta Part of the problem may be that the json in the question is invalid. Try it [on a json validator like this one](https://jsonlint.com/). – Jack Fleeting May 08 '20 at 10:43
  • Hi Jack Thanks.. jq '[.facts_hash | to_entries[] | select(.key|startswith("macaddress_")).value]' like this and it retuns the expected reult.. – Bharat Gupta May 08 '20 at 12:55
  • @BharatGupta It seems to be the same expression, doesn't it? – Jack Fleeting May 08 '20 at 13:42
  • jack in your expression there was [ ] bracket after the .facts_hash, i just removed that and it works. – Bharat Gupta May 08 '20 at 17:04
  • @BharatGupta Interesting! In theory, it shouldn't work without the brackets (it definitely works with them when I try it). It shows again how fickle jq, jsonpath, etc. are and how much they depend on where they are used. That's why I prefer working with xml and xpath... – Jack Fleeting May 08 '20 at 17:26
0

jq does the trick as recommended by jack.. and here is the clear jq expression..

jq '[.facts_hash | to_entries[] | select(.key|startswith("macaddress_")).value]'

Thanks Jack once again for your help..

Bharat Gupta
  • 127
  • 1
  • 10