Raymond Eiber
Raymond Eiber

Reputation: 39

Get unknown children of node from a JSON in JavaScript

I am currently building a tool that should extract the Core Web Vitals metrics of a URL.

With the API I can receive a JSON object which I can access with JSONPath.

I would like to use a forEach loop to input the data into the HTML-fields.

Now my question is, how can I access child nodes of a JSON without using their names?

document.querySelectorAll('[data-section^="cww"]').forEach((nodes, index) => {
    console.log(values.body.record.metrics);
});
{
    "key": {
        "origin": "https://developer.mozilla.org"
    },
    "metrics": {
        "cumulative_layout_shift": {
            "histogram": [
                {
                    "start": "0.00",
                    "end": "0.10",
                    "density": 0.9377813344003197
                },
                {
                    "start": "0.10",
                    "end": "0.25",
                    "density": 0.039611883565069506
                },
                {
                    "start": "0.25",
                    "density": 0.022606782034610366
                }
            ],
            "percentiles": {
                "p75": "0.01"
            }
        },
        "first_contentful_paint": {
            "histogram": [
                {
                    "start": 0,
                    "end": 1800,
                    "density": 0.9419767907162874
                },
                {
                    "start": 1800,
                    "end": 3000,
                    "density": 0.03741496598639458
                },
                {
                    "start": 3000,
                    "density": 0.02060824329731889
                }
            ],
            "percentiles": {
                "p75": 841
            }
        },
        "first_input_delay": {
            "histogram": [
                {
                    "start": 0,
                    "end": 100,
                    "density": 0.9863863863863849
                },
                {
                    "start": 100,
                    "end": 300,
                    "density": 0.008308308308308296
                },
                {
                    "start": 300,
                    "density": 0.0053053053053052955
                }
            ],
            "percentiles": {
                "p75": 5
            }
        },
        "largest_contentful_paint": {
            "histogram": [
                {
                    "start": 0,
                    "end": 2500,
                    "density": 0.9460068054443531
                },
                {
                    "start": 2500,
                    "end": 4000,
                    "density": 0.03467774219375491
                },
                {
                    "start": 4000,
                    "density": 0.019315452361889692
                }
            ],
            "percentiles": {
                "p75": 1135
            }
        }
    }
}

Upvotes: 0

Views: 722

Answers (1)

Serge
Serge

Reputation: 43880

try this

  var result=[];

  Object.keys(data.metrics).forEach(function (key) {
     var obj={}
     obj.metric = key;
     obj.p75 = data.metrics[key].percentiles.p75
     result.push(obj);
  });

output

[{"metric":"cumulative_layout_shift","p75":"0.01"},
{"metric":"first_contentful_paint","p75":841},
{"metric":"first_input_delay","p75":5},
{"metric":"largest_contentful_paint","p75":1135}]

Upvotes: 2

Related Questions