Convert JSON-like String to JSON

I have a String that describes an object and want to convert it into a JS Object.

String:

{
    "platform": "desktop",
    pageName: "JD Sports - Nike Air Force 1  07 LV8 Herren", //Page Title
    pageType: "product", //Page Type
    plu: "16085947_jdsportsde", //Product Code
    description: "Nike Air Force 1  07 LV8 Herren", //Product Name
    unitPrice: "110.00", //Product Price
                    category: "Herren \u003E Herrenschuhe \u003E Sneakers", //End level category
    categoryId: "jdsportsde_ct81275jdsportsde_ct81279jdsportsde_ct81356jdsportsde", //End level category Id
            sale: false, //is on sale? true/false
    brand: "Nike", //Product Brand
    ownbrand: false, //own brand product? true/false
    exclusive: false, //exclusive product? true/false
    onlineexlusive: false, //online exlcusive product? true/false
    currency:"EUR",
    variants: [
                                                            {
                            name:"41",
                            upc: "0194501189583",
                            page_id_variant: "16085947_jdsportsde.0194501189583"
                    }
                                    ,                       {
                            name:"42.5",
                            upc: "0194501189606",
                            page_id_variant: "16085947_jdsportsde.0194501189606"
                    }
                                    ,                       {
                            name:"45",
                            upc: "0194501189644",
                            page_id_variant: "16085947_jdsportsde.0194501189644"
                    }
                                    ,                       {
                            name:"47",
                            upc: "0194501189675",
                            page_id_variant: "16085947_jdsportsde.0194501189675"
                    }
                                    ,                       {
                            name:"47.5",
                            upc: "0194501189682",
                            page_id_variant: "16085947_jdsportsde.0194501189682"
                    }
                                    ,                       {
                            name:"48.5",
                            upc: "0194501189699",
                            page_id_variant: "16085947_jdsportsde.0194501189699"
                    }
                                    ,                       {
                            name:"L",
                            upc: "0194501189569",
                            page_id_variant: "16085947_jdsportsde.0194501189569"
                    }
    ]
};

Because the keys arent in quotation marks and because of the comments I can’t use JSON.parse()
How can i convert my String into an object?

4 thoughts on “Convert JSON-like String to JSON”

  1. It does look more like actual real javascript than JSON.

    Although it is often considered evil, you could use eval to parse (and execute!) it, at the risk of running uncontrolled script, either in your user’s browsers or in your backend:

    var str = `{
        "platform": "desktop",
        pageName: "JD Sports - Nike Air Force 1  07 LV8 Herren", //Page Title
        pageType: "product", //Page Type
        plu: "16085947_jdsportsde", //Product Code
        description: "Nike Air Force 1  07 LV8 Herren", //Product Name
        unitPrice: "110.00", //Product Price
                        category: "Herren \u003E Herrenschuhe \u003E Sneakers", //End level category
        categoryId: "jdsportsde_ct81275jdsportsde_ct81279jdsportsde_ct81356jdsportsde", //End level category Id
                sale: false, //is on sale? true/false
        brand: "Nike", //Product Brand
        ownbrand: false, //own brand product? true/false
        exclusive: false, //exclusive product? true/false
        onlineexlusive: false, //online exlcusive product? true/false
        currency:"EUR",
        variants: [
                                                                {
                                name:"41",
                                upc: "0194501189583",
                                page_id_variant: "16085947_jdsportsde.0194501189583"
                        }
                                        ,                       {
                                name:"42.5",
                                upc: "0194501189606",
                                page_id_variant: "16085947_jdsportsde.0194501189606"
                        }
                                        ,                       {
                                name:"45",
                                upc: "0194501189644",
                                page_id_variant: "16085947_jdsportsde.0194501189644"
                        }
                                        ,                       {
                                name:"47",
                                upc: "0194501189675",
                                page_id_variant: "16085947_jdsportsde.0194501189675"
                        }
                                        ,                       {
                                name:"47.5",
                                upc: "0194501189682",
                                page_id_variant: "16085947_jdsportsde.0194501189682"
                        }
                                        ,                       {
                                name:"48.5",
                                upc: "0194501189699",
                                page_id_variant: "16085947_jdsportsde.0194501189699"
                        }
                                        ,                       {
                                name:"L",
                                upc: "0194501189569",
                                page_id_variant: "16085947_jdsportsde.0194501189569"
                        }
                        ]
    };`;
    
    eval("obj = " + str);
    console.log(obj.pageName);
    Reply
  2. The best solution would be to fix the code that gives you this string, rather than format the ill-formatted string after it was created. Prefer fixing the causes rather than the consequences.

    If that is not possible or if for any reason you don’t want to do this, then the only solution is to use a little trick.

    First of all, let’s make your string into a JavaScript object:

    const string = `{
        "platform": "desktop",
        pageName: "JD Sports - Nike Air Force 1  07 LV8 Herren", //Page Title
        pageType: "product", //Page Type
        plu: "16085947_jdsportsde", //Product Code
        description: "Nike Air Force 1  07 LV8 Herren", //Product Name
        unitPrice: "110.00", //Product Price
                        category: "Herren > Herrenschuhe > Sneakers", //End level category
        categoryId: "jdsportsde_ct81275jdsportsde_ct81279jdsportsde_ct81356jdsportsde", //End level category Id
                sale: false, //is on sale? true/false
        brand: "Nike", //Product Brand
        ownbrand: false, //own brand product? true/false
        exclusive: false, //exclusive product? true/false
        onlineexlusive: false, //online exlcusive product? true/false
        currency:"EUR",
        variants: [
                                                                {
                                name:"41",
                                upc: "0194501189583",
                                page_id_variant: "16085947_jdsportsde.0194501189583"
                        }
                                        ,                       {
                                name:"42.5",
                                upc: "0194501189606",
                                page_id_variant: "16085947_jdsportsde.0194501189606"
                        }
                                        ,                       {
                                name:"45",
                                upc: "0194501189644",
                                page_id_variant: "16085947_jdsportsde.0194501189644"
                        }
                                        ,                       {
                                name:"47",
                                upc: "0194501189675",
                                page_id_variant: "16085947_jdsportsde.0194501189675"
                        }
                                        ,                       {
                                name:"47.5",
                                upc: "0194501189682",
                                page_id_variant: "16085947_jdsportsde.0194501189682"
                        }
                                        ,                       {
                                name:"48.5",
                                upc: "0194501189699",
                                page_id_variant: "16085947_jdsportsde.0194501189699"
                        }
                                        ,                       {
                                name:"L",
                                upc: "0194501189569",
                                page_id_variant: "16085947_jdsportsde.0194501189569"
                        }
                        ]};`
    let object
    
    eval("object = " + string)
    

    This will assign string as an object to object. One this is done you can convert the object into a valid JSON string:

    const jsonString = JSON.stringify(object)
    
    Reply
  3. First off you could use eval if you absolutely trust the source as it looks more like JS than JSON! In other words, don’t do this unless you’re 100% sure.

    var s = `{
        "platform": "desktop",
        pageName: "JD Sports - Nike Air Force 1  07 LV8 Herren", //Page Title
        pageType: "product", //Page Type
        plu: "16085947_jdsportsde", //Product Code
        description: "Nike Air Force 1  07 LV8 Herren" //Product Name
    }`;
    var obj;
    
    eval('obj = ' + s);
    console.log(obj.pageType);
    
    // and convert it to valid JSON
    var json = JSON.stringify(obj);
    console.log('JSON: ', json);
    Reply

Leave a Comment