I have this JSON generated from external (Reviews-io) script: https://widget.reviews.co.uk/rich-snippet/dist.js
JavaScript
x
24
24
1
richSnippet({
2
3
store: "www.storedigital.local",
4
sku:"6647;6647_5;6647_4;6647_3;6647_11;6647_10;6647_2;6647_1;6647_9;6647_8;6647_7;6647_6",
5
data:{
6
"url": "store.stg.gsd.local/1/silla-replica-eames.html",
7
"description": ``,
8
"mpn": "6647",
9
"offers" :[{
10
"@type":"Offer",
11
"availability": "http://schema.org/InStock",
12
"price": "559",
13
"priceCurrency": "MXN",
14
"url": "https://store.stg.gsd.localx/1/silla-replica-eames.html",
15
"priceValidUntil": "2022-05-26",
16
}],
17
"brand": {
18
"@type": "Brand",
19
"name": "Not Available",
20
}
21
}
22
23
})
24
I need to get all the string of numbers in “sku”, and then put them in another variable as same format (6647; 6647_1; 6647_2)
I try to get the numbers using this JS but doesn’t works
JavaScript
1
8
1
var skucollection = JSON.parse(richSnippet, function (key, value) {
2
if (key == "sku") {
3
return new Sku(value);
4
} else {
5
return value;
6
}
7
});
8
Can you help me check what I am doing wrong, to get this sku’s value string, please?
Advertisement
Answer
JSON.parse is not too much? ,handle it as it is internally (a JSON indeed)
JavaScript
1
32
32
1
var richSnippet = {
2
store: 'www.storedigital.local',
3
sku: '6647;6647_5;6647_4;6647_3;6647_11;6647_10;6647_2;6647_1;6647_9;6647_8;6647_7;6647_6',
4
algomas: [],
5
data: {
6
url: 'store.stg.gsd.local/1/silla-replica-eames.html',
7
description: ``,
8
mpn: '6647',
9
offers: [
10
{
11
'@type': 'Offer',
12
availability: 'http://schema.org/InStock',
13
price: '559',
14
priceCurrency: 'MXN',
15
url: 'https://store.stg.gsd.localx/1/silla-replica-eames.html',
16
priceValidUntil: '2022-05-26',
17
},
18
],
19
brand: {
20
'@type': 'Brand',
21
name: 'Not Available',
22
},
23
},
24
};
25
var test;
26
Object.keys(richSnippet).forEach((key) => {
27
if (key == 'sku') {
28
test = richSnippet[key];
29
}
30
});
31
32
console.log('test', test);