Pj Rigor Pj Rigor - 2 months ago 11x
JSON Question

Unexpected token in JSON during JSON.parse()

I am getting an error "Unexpected token in JSON at position 26" for a webRTC offer. I am using an ajax call to take the offer from a db and return it as plain text to parse into a JSON object. When I try to parse the string, I am given an "Unexpected token in JSON" error.

Here is the JSON as a plain string

o=- 552724588234335198 2 IN IP4
t=0 0
a=msid-semantic: WMS
m=application 52731 DTLS/SCTP 5000
c=IN IP4
a=candidate:2144433521 1 udp 2122257663 2002:c0a9:237::c0a9:237 52729 typ host generation 0 network-id 4
a=candidate:1068658286 1 udp 2122189567 2001::9d38:90d7:88a:6fc:52ce:a035 52730 typ host generation 0 network-id 7
a=candidate:398976708 1 udp 2122129151 52731 typ host generation 0 network-id 3
a=candidate:827492737 1 tcp 1518277887 2002:c0a9:237::c0a9:237 9 typ host tcptype active generation 0 network-id 4
a=candidate:1899074206 1 tcp 1518209791 2001::9d38:90d7:88a:6fc:52ce:a035 9 typ host tcptype active generation 0 network-id 7
a=candidate:1497635380 1 tcp 1518149375 9 typ host tcptype active generation 0 network-id 3
a=fingerprint:sha-256 01:39:AF:9A:67:87:F9:52:E9:20:3C:0D:4A:8B:A0:22:E2:D1:01:65:51:32:E1:5B:8B:9E:BC:CA:6E:DF:E2:46
a=sctpmap:5000 webrtc-datachannel 1024

and the code

url: "./php/actions.php",
type: "post",
dataType: "text",
data: {type:'retrieve', roomid:roomid},
success: function(data){
//data is plain text from string of offer retrieved from db
$('.termp').text(data); //used to check raw text data for now
var offerSDP = JSON.parse(data); //returning error when trying to convert to json
remoteConn.setRemoteDescription(new RTCSessionDescription(offerSDP)); //needs an object

I have had success using a copy and paste method before in a different application where I get raw text data and paste it into a text area and then JSON.parse(text-area.val()) and successfully set sessionDescription, but the methods are seemingly the same but yield different results.


In JSON, plain new line character is forbidden. You need to replace it with \n.

Please take a look at this topic: Multiline strings in JSON