Últimamente estoy trabajando con formato JSON.
A veces con mucha información. Por ejemplo para PageSpeed Insights, el json contiene 7,102 datos.
Para utilizar la información, la paso a un array, y entonces trabajo con los datos.
Pero cuando la información es extensa, se complica armar la llave que necesitas para acceder cierto dato. Por ejemplo para obtener el score de las audits que se hacen, el path completo del array sería:
r['lighthouseResult']['categories']['performance']['score']
Y para obtener las auditorías:
r['lighthouseResult']['categories']['performance']['auditRefs']*(56)
el (56) indica que tiene 56 «sub llaves». Y cada sub llave puede tener a su vez sub llaves. El número solo indica las sub llaves en un primer nivel.
Les dejo el código:
function map_json (string $s, mixed $json){ if (is_string($json)) { $a = json_decode($json, true); } else { $a = $json; } $all_keys = array(); $base = ''; foreach ($a as $key => $value) { $base = $s."['$key']"; if (is_array($value)) { $all_keys[] = $base . '*' . '(' . count($value) . ')'; $all_keys = array_merge($all_keys, map_json($base,json_encode($value))); }else{ $all_keys[] = $base; } } return $all_keys; }
Y les dejo una sección del resultado:
r['lighthouseResult']['categories']['performance']*(4) r['lighthouseResult']['categories']['performance']['id'] r['lighthouseResult']['categories']['performance']['title'] r['lighthouseResult']['categories']['performance']['score'] r['lighthouseResult']['categories']['performance']['auditRefs']*(56) r['lighthouseResult']['categories']['performance']['auditRefs']['0']*(5) r['lighthouseResult']['categories']['performance']['auditRefs']['0']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['acronym'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['relevantAudits']*(11) r['lighthouseResult']['categories']['performance']['auditRefs']['0']['relevantAudits']['0'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['relevantAudits']['1'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['relevantAudits']['2'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['relevantAudits']['3'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['relevantAudits']['4'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['relevantAudits']['5'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['relevantAudits']['6'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['relevantAudits']['7'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['relevantAudits']['8'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['relevantAudits']['9'] r['lighthouseResult']['categories']['performance']['auditRefs']['0']['relevantAudits']['10'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']*(5) r['lighthouseResult']['categories']['performance']['auditRefs']['1']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['acronym'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']*(17) r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['0'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['1'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['2'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['3'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['4'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['5'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['6'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['7'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['8'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['9'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['10'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['11'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['12'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['13'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['14'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['15'] r['lighthouseResult']['categories']['performance']['auditRefs']['1']['relevantAudits']['16'] r['lighthouseResult']['categories']['performance']['auditRefs']['2']*(5) r['lighthouseResult']['categories']['performance']['auditRefs']['2']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['2']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['2']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['2']['acronym'] r['lighthouseResult']['categories']['performance']['auditRefs']['2']['relevantAudits']*(9) r['lighthouseResult']['categories']['performance']['auditRefs']['2']['relevantAudits']['0'] r['lighthouseResult']['categories']['performance']['auditRefs']['2']['relevantAudits']['1'] r['lighthouseResult']['categories']['performance']['auditRefs']['2']['relevantAudits']['2'] r['lighthouseResult']['categories']['performance']['auditRefs']['2']['relevantAudits']['3'] r['lighthouseResult']['categories']['performance']['auditRefs']['2']['relevantAudits']['4'] r['lighthouseResult']['categories']['performance']['auditRefs']['2']['relevantAudits']['5'] r['lighthouseResult']['categories']['performance']['auditRefs']['2']['relevantAudits']['6'] r['lighthouseResult']['categories']['performance']['auditRefs']['2']['relevantAudits']['7'] r['lighthouseResult']['categories']['performance']['auditRefs']['2']['relevantAudits']['8'] r['lighthouseResult']['categories']['performance']['auditRefs']['3']*(5) r['lighthouseResult']['categories']['performance']['auditRefs']['3']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['3']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['3']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['3']['acronym'] r['lighthouseResult']['categories']['performance']['auditRefs']['3']['relevantAudits']*(3) r['lighthouseResult']['categories']['performance']['auditRefs']['3']['relevantAudits']['0'] r['lighthouseResult']['categories']['performance']['auditRefs']['3']['relevantAudits']['1'] r['lighthouseResult']['categories']['performance']['auditRefs']['3']['relevantAudits']['2'] r['lighthouseResult']['categories']['performance']['auditRefs']['4']*(4) r['lighthouseResult']['categories']['performance']['auditRefs']['4']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['4']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['4']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['4']['acronym'] r['lighthouseResult']['categories']['performance']['auditRefs']['5']*(4) r['lighthouseResult']['categories']['performance']['auditRefs']['5']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['5']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['5']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['5']['acronym'] r['lighthouseResult']['categories']['performance']['auditRefs']['6']*(3) r['lighthouseResult']['categories']['performance']['auditRefs']['6']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['6']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['6']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['7']*(4) r['lighthouseResult']['categories']['performance']['auditRefs']['7']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['7']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['7']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['7']['acronym'] r['lighthouseResult']['categories']['performance']['auditRefs']['8']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['8']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['8']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['9']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['9']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['9']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['10']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['10']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['10']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['11']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['11']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['11']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['12']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['12']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['12']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['13']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['13']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['13']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['14']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['14']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['14']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['15']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['15']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['15']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['16']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['16']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['16']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['17']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['17']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['17']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['18']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['18']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['18']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['19']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['19']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['19']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['20']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['20']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['20']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['21']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['21']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['21']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['22']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['22']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['22']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['23']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['23']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['23']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['24']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['24']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['24']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['25']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['25']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['25']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['26']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['26']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['26']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['27']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['27']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['27']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['28']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['28']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['28']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['29']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['29']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['29']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['30']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['30']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['30']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['31']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['31']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['31']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['32']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['32']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['32']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['33']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['33']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['33']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['34']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['34']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['34']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['35']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['35']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['35']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['36']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['36']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['36']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['37']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['37']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['37']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['38']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['38']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['38']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['39']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['39']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['39']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['40']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['40']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['40']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['41']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['41']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['41']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['42']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['42']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['42']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['43']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['43']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['43']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['44']*(2) r['lighthouseResult']['categories']['performance']['auditRefs']['44']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['44']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['45']*(3) r['lighthouseResult']['categories']['performance']['auditRefs']['45']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['45']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['45']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['46']*(3) r['lighthouseResult']['categories']['performance']['auditRefs']['46']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['46']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['46']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['47']*(3) r['lighthouseResult']['categories']['performance']['auditRefs']['47']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['47']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['47']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['48']*(3) r['lighthouseResult']['categories']['performance']['auditRefs']['48']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['48']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['48']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['49']*(3) r['lighthouseResult']['categories']['performance']['auditRefs']['49']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['49']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['49']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['50']*(3) r['lighthouseResult']['categories']['performance']['auditRefs']['50']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['50']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['50']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['51']*(3) r['lighthouseResult']['categories']['performance']['auditRefs']['51']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['51']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['51']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['52']*(3) r['lighthouseResult']['categories']['performance']['auditRefs']['52']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['52']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['52']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['53']*(3) r['lighthouseResult']['categories']['performance']['auditRefs']['53']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['53']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['53']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['54']*(3) r['lighthouseResult']['categories']['performance']['auditRefs']['54']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['54']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['54']['group'] r['lighthouseResult']['categories']['performance']['auditRefs']['55']*(3) r['lighthouseResult']['categories']['performance']['auditRefs']['55']['id'] r['lighthouseResult']['categories']['performance']['auditRefs']['55']['weight'] r['lighthouseResult']['categories']['performance']['auditRefs']['55']['group']