JSON Formatting
Hello fellow coders!
I've run into a problem. I need to format the JSON response I get from the Google PageSpeed Insights API and send it to my frontend (VueJS).
I need to loop over the objects (AvoidLandingPageRedirects or EnableGzipCompression etc.) in formattedResults (since there can be a different number of objects in each response) of the JSON request and create a new JSON format for those objects that has a higher ruleImpact than 0. Also I need to replace the curly braces with the key value pairs (ex. {{SIZE_IN_BYTES}}) of each object in formattedResults.
How can I do this?
Example of the JSON response:
{
kind: "pagespeedonline#result",
id: "https://www.stylepit.dk/",
responseCode: 200,
title: "Sko, tøj og accessories - Online mode hos STYLEPIT",
ruleGroups: {
SPEED: {
score: 83
}
},
pageStats: {
numberResources: 151,
numberHosts: 54,
totalRequestBytes: "32996",
numberStaticResources: 65,
htmlResponseBytes: "175629",
cssResponseBytes: "387875",
imageResponseBytes: "412752",
javascriptResponseBytes: "1906758",
otherResponseBytes: "32963",
numberJsResources: 25,
numberCssResources: 1
},
formattedResults: {
locale: "da",
ruleResults: {
AvoidLandingPageRedirects: {
localizedRuleName: "Undgå omdirigeringer af landingssider",
ruleImpact: 0,
groups: [
"SPEED"
],
summary: {
format: "Din side har ingen omdirigeringer. Få flere oplysninger om at {{BEGIN_LINK}}undgå omdirigeringer af destinationssiden{{END_LINK}}.",
args: [
{
type: "HYPERLINK",
key: "LINK",
value: "https://developers.google.com/speed/docs/insights/AvoidRedirects"
}
]
}
},
EnableGzipCompression: {
localizedRuleName: "Aktiver komprimering",
ruleImpact: 0.4668,
groups: [
"SPEED"
],
summary: {
format: "Komprimering af ressourcer med gzip eller deflate kan reducere antallet af bytes, der sendes over netværket."
},
urlBlocks: [
{
header: {
format: "{{BEGIN_LINK}}Aktivér komprimering{{END_LINK}} for følgende ressourcer for at reducere deres overførselstørrelse med {{SIZE_IN_BYTES}} ({{PERCENTAGE}} reduktion).",
args: [
{
type: "HYPERLINK",
key: "LINK",
value: "https://developers.google.com/speed/docs/insights/EnableCompression"
},
{
type: "BYTES",
key: "SIZE_IN_BYTES",
value: "3,9 KB"
},
{
type: "PERCENTAGE",
key: "PERCENTAGE",
value: "66 %"
}
]
},
urls: [
{
result: {
format: "Ved at komprimere {{URL}} kan der spares {{SIZE_IN_BYTES}} ({{PERCENTAGE}} reduktion).",
args: [
{
type: "URL",
key: "URL",
value: "https://dis.eu.criteo.com/dis/dis.aspx?p=10861&cb=19067057221&ref=&sc_r=1024x768&sc_d=24"
},
{
type: "BYTES",
key: "SIZE_IN_BYTES",
value: "3,9 KB"
},
{
type: "PERCENTAGE",
key: "PERCENTAGE",
value: "66 %"
}
]
}
}
]
}
]
},
Please or to participate in this conversation.