Convert string to object?

So I am trying to scrape some data from a website. And I am trying to get it's availability date which is in a section starting with //#835635. Currently, I have the HTML content of this section stripped out from the website and have a string payload like this.

//#835635
var chn_capture_status = "OFF";
var qSVariables = "";
var siteSubDirectory = "/us";
var siteUseCase = "jellycat-us";
var groupUseCase = "DEFAULT";
var currencyUseCase = "USD";
var cc_module = false;
var startTime = new Date().getTime();

var prodzoom_control = 'on';
var globalPageCookieName = 'PageJellycat2';
var siteid = 'jellycat-us'
    //alert('jellycat-us')

    var google_recaptcha_site_key = '6Ldj1SAUAAAAACV-v_0I26uqFnr8Az2jrC2GEYw_';
var stock_notify_status = '0';
var stock_notify_threshold = 0;
$(document).ready(function () {

    $('#zoomArea').fancybox();

    saveRecentlyViewed('BARM3BR', 'Bartholomew Bear', '/bartholomew-bear-barm3br/', 'BARM3BR.jpg');
    saveLastProductToCookie('BARM3BR')
});
var buy_button_hidden_instock = 'In stock - Contact us to order this item.';
var buy_button_hidden_outstock = 'OUT OF STOCK <br />Contact us to find out when this item will be next available.';
$(document).ready(function () {
    var myVariantGridEM = new variantGrid('myVariantGridEM'); // Load the variant grid class
    variants = {
        "barh2br": {
            'sequence': '27',
            'wasprice': '$40.00 USD',
            'wasprice_net': '$40.00 USD',
            'price': '$40.00 USD',
            'price_net': '$40.00 USD',
            'sale_item': 'false',
            'manufacturer_sku': '',
            'manufacturer_name': 'JELLYCAT',
            'sell': 'false',
            'stock_level': -1,
            'lead_text': '2020-12-16',
            'lead_text_summary': 'Returning to stock 3-4 weeks',
            'show_certificate': 'N',
            'cc_available': 'true',
            'option1': 'BROWN',
            'option2': 'HUGE - H19\"',
            'option3': '',
            'pf_id': 'BARM3BR',
            'monogram_code': '',
            'tailoring_code': '',
            'promo_flag': '',
            'swatch': 'BARM3BR.jpg',
            'swatchhex': '',
            'attributes': {
                "animal": ["Bear"],
                "style": ["Bartholomew"]
            },
            'downloads': {},
            'name': 'Bartholomew Bear',
            'branddesc': '',
            'pers_max_length': 0,
            'pers_colors': '',
            'pers_fonts': '',
            'pers_positions': '',
            'pers_help': '',
            'pers_cost': 0,
            'pers_cost_to_show': '$0.00 USD',
            'images': ['BARH2BR.jpg'],
            'sku': 'BARH2BR'
        },
        "barl2br": {
            'sequence': '35',
            'wasprice': '$30.00 USD',
            'wasprice_net': '$30.00 USD',
            'price': '$30.00 USD',
            'price_net': '$30.00 USD',
            'sale_item': 'false',
            'manufacturer_sku': '',
            'manufacturer_name': 'JELLYCAT',
            'sell': 'false',
            'stock_level': -2,
            'lead_text': '2020-12-11',
            'lead_text_summary': 'Returning to stock 3-4 weeks',
            'show_certificate': 'N',
            'cc_available': 'true',
            'option1': 'BROWN',
            'option2': 'LARGE - H14\"',
            'option3': '',
            'pf_id': 'BARM3BR',
            'monogram_code': '',
            'tailoring_code': '',
            'promo_flag': '',
            'swatch': 'BARM3BR.jpg',
            'swatchhex': '',
            'attributes': {
                "animal": ["Bear"],
                "style": ["Bartholomew"]
            },
            'downloads': {},
            'name': 'Bartholomew Bear',
            'branddesc': '',
            'pers_max_length': 0,
            'pers_colors': '',
            'pers_fonts': '',
            'pers_positions': '',
            'pers_help': '',
            'pers_cost': 0,
            'pers_cost_to_show': '$0.00 USD',
            'images': ['BARL2BR.jpg'],
            'sku': 'BARL2BR'
        },
        "barm3br": {
            'sequence': '50',
            'wasprice': '$22.50 USD',
            'wasprice_net': '$22.50 USD',
            'price': '$22.50 USD',
            'price_net': '$22.50 USD',
            'sale_item': 'false',
            'manufacturer_sku': '',
            'manufacturer_name': 'JELLYCAT',
            'sell': 'false',
            'stock_level': -6,
            'lead_text': '2021-02-09',
            'lead_text_summary': 'Returning to stock 2-3 months',
            'show_certificate': 'N',
            'cc_available': 'true',
            'option1': 'BROWN',
            'option2': 'MEDIUM - H11\"',
            'option3': '',
            'pf_id': 'BARM3BR',
            'monogram_code': '',
            'tailoring_code': '',
            'promo_flag': '',
            'swatch': 'BARM3BR.jpg',
            'swatchhex': '',
            'attributes': {
                "animal": ["Bear"],
                "style": ["Bartholomew"]
            },
            'downloads': {},
            'name': 'Bartholomew Bear',
            'branddesc': '',
            'pers_max_length': 0,
            'pers_colors': '',
            'pers_fonts': '',
            'pers_positions': '',
            'pers_help': '',
            'pers_cost': 0,
            'pers_cost_to_show': '$0.00 USD',
            'images': [],
            'sku': 'BARM3BR'
        }
    }

    settings = {
        "option1": "Please select an option.",
        "option1_alert": "Please select an option from the dropdown",
        "option2": "Please select an option.",
        "option2_alert": "Please select an option from the dropdown",
        "vat_text": "Inc VAT",
        "includes_vat": "1",
        "hidebasketbutton": "0",
        "variantarea": "variant-grid-area",
        "image": "mainImage",
        "spinseturl": "SPINSET_URL_HTTPS",
        "defaultsku": "barm3br",
        "pagetype": "",
        "alternatives": {
            "BARM3BR": {
                "image": ["BARM3BR.jpg", "BARM3BR_1.jpg", "BARM3BR_2.jpg", "BARM3BR_3.jpg"]
            }
        },
        mappings: {
            "price": "Price (Inc VAT)",
            "animal": "Animal",
            "author": "Authors",
            "blog_categories": "Categories (Blog)",
            "brand": "Brand",
            "category_list": "Categories",
            "date": "Archive",
            "rss": "RSS",
            "size": "Size",
            "style": "Style",
            "tags": "Tags"
        },
        show_price: "true",
        option1: "Please select an option."
    }
    myVariantGridEM.loadVariants(variants);
    myVariantGridEM.loadSettings(settings);
    myVariantGridEM.renderGrid();
});
var level2cat = 'COLLECTIONS';
var level3cat = '';
$(document).ready(function () {
    cdnPull_Function();
});
function cdnPull_Function() {
    cdnPull('CurrentCustomerJellycat2', true);
    cdnPullPage('PageJellycat2', 'product');
}
 <  / script >
 < script type = "text/javascript" >
    var renderCallBack = function () {
    var recaptcha1;
    if ($('#recaptcha1').length == 1) {
        recaptcha1 = grecaptcha.render('recaptcha1', {
                'sitekey': google_recaptcha_site_key, //Replace this with your Site key
                'theme': 'light'
            });
    }
};

And I would like to somehow format the string into an object so I can extract more detailed data from it.

I think it would help if you formatted the post correctly.

Use the </> button to paste the HTML code.

I would like to try and help you, but my machine is kind of dead just now and I am using a different one and don't have the usual set of tools to help you.

Just edited the format.

Try this

[{"id":"a9e8204.95171e","type":"inject","z":"a1dea2b.76223e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":60,"wires":[["496d3d5d.0502ec"]]},{"id":"496d3d5d.0502ec","type":"http request","z":"a1dea2b.76223e","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://www.jellycat.com/us/edward-bear-eb3br/","tls":"","persist":false,"proxy":"","authType":"","x":330,"y":80,"wires":[["687f1de1.4f3ff4"]]},{"id":"687f1de1.4f3ff4","type":"function","z":"a1dea2b.76223e","name":"","func":"msg.payload = msg.payload.split(\"settings =\",1)[0].split(\"variants =\")[1].replace(/'/g,'\"');\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":430,"y":140,"wires":[["74de736f.8651bc","90e09665.2d0b98"]]},{"id":"74de736f.8651bc","type":"json","z":"a1dea2b.76223e","name":"","property":"payload","action":"","pretty":false,"x":510,"y":220,"wires":[["90e09665.2d0b98"]]},{"id":"90e09665.2d0b98","type":"debug","z":"a1dea2b.76223e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":680,"y":160,"wires":[]}]

WOW! It works! Please give me a bit more explanation.
From what I understand, by replacing ' with ", the build-in JSON parser will be able to understand the script? How? Is it because it was JavaScript not JSON? Sorry my terminology and understanding of JavaScript and JSON are very limited.

first i split the returned http request text on "settings =" and retuned the first value.
then i split that returned value on "varients =" and returned the last value.
As ' are not valid json , i then replaced them all with ", making it a valid json string.

1 Like

Why do you put .split("settings =",1)? So it only split once incase there are other "settings ="?

yes only the first is needed, waste of time letting it continue, it could be omitted, just a preference.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.