5

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

 {"type":"offer","sdp":"v=0
 o=- 552724588234335198 2 IN IP4 127.0.0.1
 s=-
 t=0 0
 a=msid-semantic: WMS
 m=application 52731 DTLS/SCTP 5000
 c=IN IP4 192.169.2.55
 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 192.169.2.55 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 192.169.2.55 9 typ host tcptype active generation 0 network-id 3
 a=ice-ufrag:EHtolesxvVPp2FqI
 a=ice-pwd:CrcvrgKTp6lUbUA81nlGeTFr
 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=setup:actpass
 a=mid:data
 a=sctpmap:5000 webrtc-datachannel 1024
 "}

and the code

$.ajax({
    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.

Pj Rigor
  • 391
  • 6
  • 17
  • can you update the question to remove the comments within the raw json string so we can see exactly what the string is? – Duly Kinsky Jul 24 '16 at 06:39
  • Could you please post exact `json` response? – Guruprasad J Rao Jul 24 '16 at 06:42
  • Full text string response posted, I used ajax to return a string on success so that I can parse it afterwards. This is after I tried returning a json object in data but the RTCSessionDescription(data) would not work because it said that the passed argument was not an object. – Pj Rigor Jul 24 '16 at 06:45

1 Answers1

5

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

Community
  • 1
  • 1
Robo Robok
  • 21,132
  • 17
  • 68
  • 126
  • I see, now that I compare the two applications, there are "\n", "\r" characters. how would I output those or upon receiving the string or how would I send the object to the database preserving the special characters? – Pj Rigor Jul 24 '16 at 06:55
  • Just replace it when you receive it, like this: `JSON.parse(data.replace(/[\r]?[\n]/g, '\\n'))` – Robo Robok Jul 24 '16 at 07:00
  • Yes this solution worked. It seems as if the newline character was put in there to pretty things up, but when converting to json you would need to explicitly have the \n. So I assume there is a difference between new line and \n? – Pj Rigor Jul 24 '16 at 07:18
  • \n is a slash followed by the n letter and new line is new line :) It's part of JSON standard that new line BYTE cannot be inside a value. It is perfectly fine to have this BYTE between properties though, any place where it's not processed as a value. – Robo Robok Jul 24 '16 at 07:25