• Errors when reading JSON string that includes carriage returns or new lines

    By Cesar Mugnatto 2 decades ago

    I used a valid JSON structure on multilines (because it's easier for me to read and edit) and tried to run it through the ls.snapps.JSONReader class and received errors. After reformatting the string to remove all new lines and carriage returns, but not otherwise changing the structure (keeping curly braces and brackets, quotes, etc. as they were), the process completed normally.

    • Sample code to illustrate bug

      By Cesar Mugnatto 2 decades ago

      Option Public

      Option Declare

      Use "ls.snapps.JSONReader"

      Sub Initialize

      Dim INPUTJSONSTRING1 As String 'Will contain mono-line string<br/>
      Dim INPUTJSONSTRING2 As String 'Will contain multi-line string<br/>
      Dim jsonReader As JSONReader<br/>
      Dim vResults As Variant<br/>
      Dim vPieces As Variant<br/>
      <br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |{ &quot;people&quot; : [{&quot;Name&quot; : &quot;CN=John Bigboote/O=Yoyodyne&quot; , &quot;MultiValueDates&quot; : [1/4/2009 , 1/11/2009 , 1/18/2009 , 1/25/2009 , 2/1/2009|<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; | , 2/8/2009 , 2/15/2009 , 2/22/2009 , 3/1/2009 , 3/8/2009 , 3/15/2009 , 3/22/2009 , 3/29/2009 , 4/5/2009 , 4/12/2009 , 4/19/2009 , 4/26/2009] , |<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |&quot;MultiValueDoubles&quot; : [3.50 , 3.50 , 3.50 , 3.50 , 2.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00]} ,{&quot;Name&quot; |<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |: &quot;CN=John Smallberries/O=Yoyodyne&quot; , &quot;MultiValueDates&quot; : [1/4/2009 , 1/11/2009 , 1/18/2009 , 1/25/2009 , 2/1/2009 , 2/8/2009 , 2/15/2009 , 2/22/2009 |<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |, 3/1/2009 , 3/8/2009 , 3/15/2009 , 3/22/2009 , 3/29/2009 , 4/5/2009 , 4/12/2009 , 4/19/2009 , 4/26/2009] , &quot;MultiValueDoubles&quot; : [2.31 , 2.31 , 2.31 |<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |, 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31]} ,{&quot;Name&quot; : &quot;CN=John Yaya/O=Yoyodyne&quot; , |<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |&quot;MultiValueDates&quot; : [1/4/2009 , 1/11/2009 , 1/18/2009 , 1/25/2009 , 2/1/2009 , 2/8/2009 , 2/15/2009 , 2/22/2009 , 3/1/2009 , 3/8/2009 , 3/15/2009 , |<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |3/22/2009 , 3/29/2009 , 4/5/2009 , 4/12/2009 , 4/19/2009] , &quot;MultiValueDoubles&quot; : [2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 |<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |, 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31]} ,{&quot;Name&quot; : &quot;CN=John Manyjars/O=Yoyodyne&quot; , &quot;MultiValueDates&quot; : [1/4/2009 , 1/11/2009 , 1/18/2009 , |<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |1/25/2009 , 2/1/2009 , 2/8/2009 , 2/15/2009 , 2/22/2009 , 3/1/2009 , 3/8/2009 , 3/15/2009 , 3/22/2009 , 3/29/2009 , 4/5/2009 , 4/12/2009 , 4/19/2009 , |<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |4/26/2009] , &quot;MultiValueDoubles&quot; : [2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , |<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |-36.96]} ,{&quot;Name&quot; : &quot;CN=John Whorfin/O=Yoyodyne&quot; , &quot;MultiValueDates&quot; : [1/4/2009 , 1/11/2009 , 1/18/2009 , 1/25/2009 , 2/1/2009 , 2/8/2009 , 2/15/2009 |<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |, 2/22/2009 , 3/1/2009 , 3/8/2009 , 3/15/2009 , 3/22/2009 , 3/29/2009 , 4/5/2009 , 4/12/2009 , 4/19/2009 , 4/26/2009] , &quot;MultiValueDoubles&quot; : [2.31 , |<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31]} ,{&quot;Name&quot; : &quot;CN=John Parker/O=Yoyodyne&quot; |<br/>
      INPUTJSONSTRING1 = INPUTJSONSTRING1 &amp; |, &quot;MultiValueDates&quot; : [4/26/2009] , &quot;MultiValueDoubles&quot; : [0.00]}]}|<br/>
      <br/>
      'The string below is structurally identical to the string above (from a JSON perspective), except it contains carriage returns that allow for easier reading and editing<br/>
      <br/>
      INPUTJSONSTRING2 = |{ &quot;people&quot; : [<br/>
      

      {

      "Name" : "CN=John Bigboote/O=Yoyodyne" ,

      "MultiValueDates" : [1/4/2009 , 1/11/2009 , 1/18/2009 , 1/25/2009 , 2/1/2009 , 2/8/2009 , 2/15/2009 , 2/22/2009 , 3/1/2009 , 3/8/2009 , 3/15/2009 , 3/22/2009 , 3/29/2009 , 4/5/2009 , 4/12/2009 , 4/19/2009 , 4/26/2009] ,

      "MultiValueDoubles" : [3.50 , 3.50 , 3.50 , 3.50 , 2.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00]

      } ,

      {

      "Name" : "CN=John Smallberries/O=Yoyodyne" ,

      "MultiValueDates" : [1/4/2009 , 1/11/2009 , 1/18/2009 , 1/25/2009 , 2/1/2009 , 2/8/2009 , 2/15/2009 , 2/22/2009 , 3/1/2009 , 3/8/2009 , 3/15/2009 , 3/22/2009 , 3/29/2009 , 4/5/2009 , 4/12/2009 , 4/19/2009 , 4/26/2009] ,

      "MultiValueDoubles" : [2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31]

      } ,

      {

      "Name" : "CN=John Yaya/O=Yoyodyne" ,

      "MultiValueDates" : [1/4/2009 , 1/11/2009 , 1/18/2009 , 1/25/2009 , 2/1/2009 , 2/8/2009 , 2/15/2009 , 2/22/2009 , 3/1/2009 , 3/8/2009 , 3/15/2009 , 3/22/2009 , 3/29/2009 , 4/5/2009 , 4/12/2009 , 4/19/2009] ,

      "MultiValueDoubles" : [2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31]

      } ,

      {

      "Name" : "CN=John Manyjars/O=Yoyodyne" ,

      "MultiValueDates" : [1/4/2009 , 1/11/2009 , 1/18/2009 , 1/25/2009 , 2/1/2009 , 2/8/2009 , 2/15/2009 , 2/22/2009 , 3/1/2009 , 3/8/2009 , 3/15/2009 , 3/22/2009 , 3/29/2009 , 4/5/2009 , 4/12/2009 , 4/19/2009 , 4/26/2009] ,

      "MultiValueDoubles" : [2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , -36.96]

      } ,

      {

      "Name" : "CN=John Whorfin/O=Yoyodyne" ,

      "MultiValueDates" : [1/4/2009 , 1/11/2009 , 1/18/2009 , 1/25/2009 , 2/1/2009 , 2/8/2009 , 2/15/2009 , 2/22/2009 , 3/1/2009 , 3/8/2009 , 3/15/2009 , 3/22/2009 , 3/29/2009 , 4/5/2009 , 4/12/2009 , 4/19/2009 , 4/26/2009] ,

      "MultiValueDoubles" : [2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31 , 2.31]

      } ,

      {

      "Name" : "CN=John Parker/O=Yoyodyne" ,

      "MultiValueDates" : [4/26/2009] ,

      "MultiValueDoubles" : [0.00]

      }

      ]}|

      <br/>
      Set jsonReader = New JSONReader<br/>
      Set vResults = jsonReader.Parse(INPUTJSONSTRING1)   'this is a JSONObject (so don't forget to put the Set)<br/>
      vPieces = vResults.Items<br/>
      <br/>
      Stop<br/>
      Msgbox Typename(vPieces)<br/>
      <br/>
      Set vResults = jsonReader.Parse(INPUTJSONSTRING2)   'this is a JSONObject (so don't forget to put the Set)<br/>
      vPieces = vResults.Items<br/>
      <br/>
      Stop<br/>
      Msgbox Typename(vPieces)<br/>
      

      End Sub