I have the following request body which i need to parse to json. I need to parse a payload
field (which is a json with a lot of trash in it) to the proper JSON object (it's a result of console.log(req)
):
{ payload: '{\\n \\"taskDueDate\\": \\"No due\\",\\n \\"oldTaskMilestone\\": null,\\n \\"isUpdatedTask\\": \\"true\\",\\n \\"oldTaskAssignee\\": null,\\n \\"statusType\\": \\"OPEN\\",\\n \\"oldTaskVisibility\\": null,\\n \\"isEstimationUpdated\\": \\"false\\",\\n \\"invokerEmail\\": \\"mike@domain\\",\\n \\"oldTaskStatus\\": \\"Resolved\\",\\n \\"projectId\\": \\"61193\\",\\n \\"taskContent\\": \\"Add god to monit background processes\\",\\n \\"taskAssignee\\": \\"Mike B.\\",\\n \\"invokerId\\": \\"38073\\",\\n \\"isLabelsUpdated\\": \\"false\\",\\n \\"taskLabels\\": \\"Improvement\\",\\n \\"isAssignmentUpdated\\": \\"false\\",\\n \\"oldTaskEstimation\\": null,\\n \\"isVisibilityUpdated\\": \\"false\\",\\n \\"isStatusUpdated\\": \\"true\\",\\n \\"isMilestoneUpdated\\": \\"false\\",\\n \\"domain\\": \\"xxx\\",\\n \\"invokerSmallAvatarURL\\": \\"xxx\\",\\n \\"invoker\\": \\"Mike B.\\",\\n \\"taskId\\": \\"33\\",\\n \\"accountURL\\": \\"xx\\",\\n \\"taskAuthor\\": \\"Mike B.\\",\\n \\"isTimeEntryAdded\\": \\"false\\",\\n \\"unsubscribeURL\\": \\"xxx\\",\\n \\"oldTaskPriority\\": null,\\n \\"oldTaskDueDate\\": null,\\n \\"projectURL\\": \\"xxx\\",\\n \\"taskMilestone\\": \\"Not planned\\",\\n \\"taskPriority\\": \\"HIGH\\",\\n \\"taskTitle\\": \\"Start using god gem\\",\\n \\"oldTaskLabels\\": null,\\n \\"isPriorityUpdated\\": \\"false\\",\\n \\"taskURL\\": \\"xxx\\",\\n \\"taskStatus\\": \\"Open\\",\\n \\"subdomain\\": \\"xx\\",\\n \\"invokerProfileURL\\": \\"xx\\",\\n \\"statusLabel\\": \\"reopened\\",\\n \\"taskEstimation\\": \\"Not estimated\\",\\n \\"isNewTask\\": \\"false\\",\\n \\"isAttachmentsUpdated\\": \\"false\\",\\n \\"projectName\\": \\"xx\\",\\n \\"taskVisibility\\": \\"ALL\\",\\n \\"isDueDateUpdated\\": \\"false\\"\\n}' }
To do so, i'm using the following code:
payload = req.body['payload']
JSON.parse(payload)
Such code gives me an error:
Syntax error: unexpected token \
What's interesting, when i'll dump a content of payload
var to console and past it in Chrome Dev Console using JSON.parse(my_copied_json_string)
, it works just fine.
Can you please advice me why is that possibly behaving like that and what can i do to understand and fix that strange issue?
JSFiddle demonstrating that issue: http://jsfiddle.net/7PZD9/5/