6 Replies Latest reply on Mar 23, 2018 9:16 PM by 3669712

    SyntaxError: Unexpected end of JSON input

    3669712

      Hi,

       

      I am trying to use JSON.parse() to input a text from a file. However, My modle is

       

      define(['ojs/ojcore', 'knockout', 'jquery','text!data/data.json','ojs/ojselectcombobox', 'ojs/ojchart'],

      function(oj, ko, $,file) {

       

          function DashboardViewModel() {

            var self = this;

            self.val = ko.observable("pie");

            self.val = ko.observable("line");

           

             self.stackValue = ko.observable('off');

              self.orientationValue = ko.observable('vertical');

             

              /* chart data */

              var barSeries = JSON.parse(file);

              var barGroups = ["Group A", "Group B"];

        

              self.barSeriesValue = ko.observableArray(barSeries);

              self.barGroupsValue = ko.observableArray(barGroups);

       

      and the data.json file is below and I don't see any errors in it!

       

       

      [{

              "name": "Series 1",

              "items": [42, 34]

          },

          {

              "name": "Series 2",

              "items": [55, 30]

          },

          {

              "name": "Series 3",

              "items": [36, 50]

          },

          {

              "name": "Series 4",

              "items": [22, 46]

          },

          {

              "name": "Series 5",

              "items": [22, 46]

          }]

       

       

      Here is the browser's error:

       

       

      Navigated to http://localhost:8383/myFirstJET/index.html

      text.js:325 GET http://localhost:8383/myFirstJET/js/data/data.json net::ERR_EMPTY_RESPONSE

      text.get @ text.js:325

      load @ text.js:204

      (anonymous) @ require.js:1095

      ...

      (anonymous) @ require.js:1460

      setTimeout (async)

      req.nextTick @ require.js:1815

      localRequire @ require.js:1449

      requirejs @ require.js:1797

      (anonymous) @ main.js:54

      ojcore.js:272 SyntaxError: Unexpected end of JSON input

          at JSON.parse (<anonymous>)

          at new DashboardViewModel (dashboard.js:20)

          at dashboard.js:88

          at Object.execCb (require.js:1696)

          at Module.check (require.js:878)

          at Module.<anonymous> (require.js:1139)

          at require.js:134

          at require.js:1189

          at each (require.js:59)

          at Module.emit (require.js:1188)

       

      Any help would be greatly appreciated.


      CS

       

       

        • 1. Re: SyntaxError: Unexpected end of JSON input
          Andrew Bennett

          Hi,

           

          Before you have the JSON.parse what output do you get if you console.log file?

           

          Also, when you added the data.json file were you already serving your application? I've seen it when if you're already serving your application and then add a JSON file it will not copy over as part of the livereload. Have a go just stopping your application and running ojet serve again, you might find that just fixes it.

           

          Cheers,

          Andy

          • 2. Re: SyntaxError: Unexpected end of JSON input
            Duncan Mills-Oracle

            Plus always check for the "invisible" copy-paste artefact - the usual one is where you copy from some doc and get "smart quotes" rather than real quotes - that can be real hard to spot

            • 3. Re: SyntaxError: Unexpected end of JSON input
              3669712

              This is the output of the Browser Log:

              Debugging session with browser was closed.

              Debugging session with browser was closed.

              Debugging session with browser was closed.

              Debugging session with browser was closed.

              Debugging session with browser was closed.

              Debugging session with browser was closed.

              ojModule failed to load viewModels/dashboard (07:51:01:770 | error, null)

                at web/js/libs/oj/v4.2.0/debug/ojcore.js:272

              SyntaxError: Unexpected end of JSON input (07:51:01:772 | error, null)

                at web/js/libs/oj/v4.2.0/debug/ojcore.js:272

              ojModule failed to load viewModels/dashboard (07:51:03:867 | error, null)

                at web/js/libs/oj/v4.2.0/debug/ojcore.js:272

              SyntaxError: Unexpected end of JSON input (07:51:03:868 | error, null)

                at web/js/libs/oj/v4.2.0/debug/ojcore.js:272

              ojModule failed to load viewModels/dashboard (07:51:07:649 | error, null)

                at web/js/libs/oj/v4.2.0/debug/ojcore.js:272

              SyntaxError: Unexpected end of JSON input (07:51:07:650 | error, null)

                at web/js/libs/oj/v4.2.0/debug/ojcore.js:272

               

              I think I added the file while I was running. Now what is the fix please?

              • 4. Re: SyntaxError: Unexpected end of JSON input
                3669712

                I don't hink this is the error. I put all the quotes myself and it was not copy and paste.

                • 5. Re: SyntaxError: Unexpected end of JSON input
                  Andrew Bennett

                  What happens when you actually try and access http://localhost:8383/myFirstJET/js/data/data.json  directly in the browser?

                   

                  I did a quick test and it's definitely recognizing the json file is there, otherwise it'd be complaining with requireJs. In my test I had an empty json file and got exactly the same console error, so for some reason it's failing to get the file and you're trying to parse an empty object. What I didn't get is the error net::ERR_EMPTY_RESPONSE, are you hosting this file within your JET application or somewhere different?

                   

                  Cheers,

                  Andy

                  • 6. Re: SyntaxError: Unexpected end of JSON input
                    3669712

                    I am sorry. The data.json was outside js folder! Thank you Andrew and everyone!