Tengo debajo de json quiero sacar toda la lista de macaddress en una matriz de jason ¿cómo lo hago?

{
  "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
}
0
Bharat Gupta 7 may. 2020 a las 15:18

3 respuestas

Jq hace el truco recomendado por jack ... y aquí está la clara expresión de jq ...

Jq '[.facts_hash | to_entries [] | seleccione (.key | comienza con ("macaddress _")). value] '

Gracias Jack una vez más por tu ayuda.

0
Bharat Gupta 8 may. 2020 a las 12:57

Si puedes usar jq, esta expresión

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

Debería generar esta matriz:

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

Otra opción sería convertir el json a xml y usar xpath en él.

0
Jack Fleeting 8 may. 2020 a las 02:43

Puede usar Object.keys para iterar la clave en el objeto, startsWith para verificar el texto de la dirección mac y usar reduce para mostrar su requerimiento como

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

O

  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);
1
Hien Nguyen 8 may. 2020 a las 04:06