简体   繁体   中英

Javascript JSON field has \" after each pushed JSON string

I have a variable with the following string value:

{"title":"Zebra Long Maxi Dress","subtitle":"Unique and comfortable Maxi dress that is easy to wear and fun to be seen in.","quantity":1,"price":150,"currency":"RON","image_url":"https://dl.airtable.com/1gGAho3dQ8uQfWpa5pEV_anne-1-(8).png"},{"title":"Blue fan dress","subtitle":"This blue fan dress will blow people away. With its custom cut design you will always stand out.\n","quantity":1,"price":100,"currency":"RON","image_url":"https://dl.airtable.com/oerRxGtZRuqnI9afPmn0_anne-1-(35).png"}

I am pushing this to the elements part of the below structure like this :

var messageData = { 
                    recipient : {
                        id: sender   
                    },
                    message:{
                        attachment:{
                            type:"template",
                            payload:{
                                template_type:"receipt",
                                recipient_name: clientName,
                                order_number: orderNumber,
                                currency:"RON",
                                payment_method:"Rambus",        
                                //order_url:"http://www.facebook.com",
                                //timestamp:"1428444852", 
                                elements:[],
                                address:{
                                    street_1:address,
                                    city:"Bucharest",
                                    postal_code:"0",
                                    state:"_",
                                    country:"RO"
                                },
                                summary:{
                                    subtotal:0,
                                    shipping_cost:0,
                                    total_tax:0,
                                    total_cost:totalCost
                                },
                                adjustments:[]
                            } // payload
                        } // attachment
                    } // message
                }; // messageData 

messageData.message.attachment.payload.elements.push(JSON.parse(elementArrayItemJson));

and I am getting the following output for the messageData variable, as you can see there is \\" where there should only be " and also "elements":["{\\"title\\":\\"Zebra Long Maxi Dress\\" has an extra "

I'm not sure what to do to get the JSON to look correct.

{"recipient":{"id":"1588309797861804"},"message":{"attachment":{"type":"template","payload":{"template_type":"receipt","recipient_name":"Ethan Richardson","order_number":"70128","currency":"RON","payment_method":"Rambus","elements":["{\"title\":\"Zebra Long Maxi Dress\",\"subtitle\":\"Unique and comfortable Maxi dress that is easy to wear and fun to be seen in.\",\"quantity\":1,\"price\":150,\"currency\":\"RON\",\"image_url\":\"https://dl.airtable.com/1gGAho3dQ8uQfWpa5pEV_anne-1-(8).png\"}","{\"title\":\"Blue fan dress\",\"subtitle\":\"This blue fan dress will blow people away. With its custom cut design you will always stand out.\\n\",\"quantity\":1,\"price\":100,\"currency\":\"RON\",\"image_url\":\"https://dl.airtable.com/oerRxGtZRuqnI9afPmn0_anne-1-(35).png\"}"],"address":{"street_1":"Flat 35 Rossetti Court Byron Road","city":"Bucharest","postal_code":"0","state":"_","country":"RO"},"summary":{"subtotal":0,"shipping_cost":0,"total_tax":0,"total_cost":250},"adjustments":[]}}}}

Since your original variable is a string and not an object, it is being inserted into the new object as a string. The \\" issue is just escaping the quotes so as not to close the string prematurely.

Convert your string into a JSON object to add it to the new object:

JSON.parse(json_string);

Your variable elementArrayItemJson is an invalid JSON object. It has 2 root elements so JSON.parse is going to get caught in a Syntax Error due to the , .

The current string shows as follow after stringify:

{
    "title": "Zebra Long Maxi Dress",
    "subtitle": "Unique and comfortable Maxi dress that is easy to wear and fun to be seen in.",
    "quantity": 1,
    "price": 150,
    "currency": "RON",
    "image_url": "https://dl.airtable.com/1gGAho3dQ8uQfWpa5pEV_anne-1-(8).png"
}, {
    "title": "Blue fan dress",
    "subtitle": "This blue fan dress will blow people away. With its custom cut design you will always stand out.\n",
    "quantity": 1,
    "price": 100,
    "currency": "RON",
    "image_url": "https://dl.airtable.com/oerRxGtZRuqnI9afPmn0_anne-1-(35).png"
}

The },{ shows that it contains 2 unnamed elements in your string. So some options that you can do is to split the string by doing so:

elementArrayItemJson.replace('},{', '}\n{');

Then split by new line (\\n) to get an array of valid JSON strings:

newElementArrayItemJson.split('\n');

Then you can push each of the elements using JSON.parse(newElementArrayItemJson[0])

This solution can be simplified quite a bit but it will show the error.

EDIT: Assuming your variable elementArrayItemJson is surrounded by single quotes since it is a string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM