Skip to content
Advertisement

Get data from RichSnippet JSON and set the same string into other variable

I have this JSON generated from external (Reviews-io) script: https://widget.reviews.co.uk/rich-snippet/dist.js

    richSnippet({

        store: "www.storedigital.local",
        sku:"6647;6647_5;6647_4;6647_3;6647_11;6647_10;6647_2;6647_1;6647_9;6647_8;6647_7;6647_6",
        data:{
          "url": "store.stg.gsd.local/1/silla-replica-eames.html",
          "description": ``,
          "mpn": "6647",
          "offers" :[{
            "@type":"Offer",
            "availability": "http://schema.org/InStock",
            "price": "559",
            "priceCurrency": "MXN",
            "url": "https://store.stg.gsd.localx/1/silla-replica-eames.html",
            "priceValidUntil": "2022-05-26",
          }],
          "brand": {
           "@type": "Brand",
           "name": "Not Available",
         }
        }

    })

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

var skucollection = JSON.parse(richSnippet, function (key, value) {
   if (key == "sku") {
     return new Sku(value);
    } else {
     return value;
    }
});

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)

var richSnippet = {
  store: 'www.storedigital.local',
  sku: '6647;6647_5;6647_4;6647_3;6647_11;6647_10;6647_2;6647_1;6647_9;6647_8;6647_7;6647_6',
  algomas: [],
  data: {
    url: 'store.stg.gsd.local/1/silla-replica-eames.html',
    description: ``,
    mpn: '6647',
    offers: [
      {
        '@type': 'Offer',
        availability: 'http://schema.org/InStock',
        price: '559',
        priceCurrency: 'MXN',
        url: 'https://store.stg.gsd.localx/1/silla-replica-eames.html',
        priceValidUntil: '2022-05-26',
      },
    ],
    brand: {
      '@type': 'Brand',
      name: 'Not Available',
    },
  },
};
var test;
Object.keys(richSnippet).forEach((key) => {
  if (key == 'sku') {
    test = richSnippet[key];
  }
});

console.log('test', test);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement