Extra text when using jsondecode












0















I am trying to come to the point where I create a graph on a given data that I am supposed to read from a text file.

So I use in my code fopen to open the text file, textscan to scan it, than make a string out of it and by using split I want to cut of the first part of every line and use the second part so that I can decode it into json and then use the information.

So my text file consists of two lines of information:




123456.99 :: working completed: result=0 , data ="{"day":"monday", "breakfast":"sandwich"}"
123456.99 :: working completed: result=0 , data ="{"day":"tuesday", "breakfast":"bread"}"




The first part of my code:



fileID  = fopen('test1');
text = textscan(fileID, '%s', 'delimiter','n','whitespace','');
strLog = string(text{1});
res = split(strLog, "data =");
json_str = res(:, 2)


And as a result I get a 2x1 string array. Output:




json_str =



2×1 string array



""{"day":"monday", "breakfast":"sandwich"}""
""{"day":"tuesday", "breakfast":"bread"}""



This is where I got stuck.

My first idea was to call cellfun and apply jsondecode.
But I got




Error using jsondecode JSON syntax error at line 1, column 4
(character 4): extra text.




But it makes no sence to me, since that should be the " from "day" which for json should be okay!?










share|improve this question



























    0















    I am trying to come to the point where I create a graph on a given data that I am supposed to read from a text file.

    So I use in my code fopen to open the text file, textscan to scan it, than make a string out of it and by using split I want to cut of the first part of every line and use the second part so that I can decode it into json and then use the information.

    So my text file consists of two lines of information:




    123456.99 :: working completed: result=0 , data ="{"day":"monday", "breakfast":"sandwich"}"
    123456.99 :: working completed: result=0 , data ="{"day":"tuesday", "breakfast":"bread"}"




    The first part of my code:



    fileID  = fopen('test1');
    text = textscan(fileID, '%s', 'delimiter','n','whitespace','');
    strLog = string(text{1});
    res = split(strLog, "data =");
    json_str = res(:, 2)


    And as a result I get a 2x1 string array. Output:




    json_str =



    2×1 string array



    ""{"day":"monday", "breakfast":"sandwich"}""
    ""{"day":"tuesday", "breakfast":"bread"}""



    This is where I got stuck.

    My first idea was to call cellfun and apply jsondecode.
    But I got




    Error using jsondecode JSON syntax error at line 1, column 4
    (character 4): extra text.




    But it makes no sence to me, since that should be the " from "day" which for json should be okay!?










    share|improve this question

























      0












      0








      0








      I am trying to come to the point where I create a graph on a given data that I am supposed to read from a text file.

      So I use in my code fopen to open the text file, textscan to scan it, than make a string out of it and by using split I want to cut of the first part of every line and use the second part so that I can decode it into json and then use the information.

      So my text file consists of two lines of information:




      123456.99 :: working completed: result=0 , data ="{"day":"monday", "breakfast":"sandwich"}"
      123456.99 :: working completed: result=0 , data ="{"day":"tuesday", "breakfast":"bread"}"




      The first part of my code:



      fileID  = fopen('test1');
      text = textscan(fileID, '%s', 'delimiter','n','whitespace','');
      strLog = string(text{1});
      res = split(strLog, "data =");
      json_str = res(:, 2)


      And as a result I get a 2x1 string array. Output:




      json_str =



      2×1 string array



      ""{"day":"monday", "breakfast":"sandwich"}""
      ""{"day":"tuesday", "breakfast":"bread"}""



      This is where I got stuck.

      My first idea was to call cellfun and apply jsondecode.
      But I got




      Error using jsondecode JSON syntax error at line 1, column 4
      (character 4): extra text.




      But it makes no sence to me, since that should be the " from "day" which for json should be okay!?










      share|improve this question














      I am trying to come to the point where I create a graph on a given data that I am supposed to read from a text file.

      So I use in my code fopen to open the text file, textscan to scan it, than make a string out of it and by using split I want to cut of the first part of every line and use the second part so that I can decode it into json and then use the information.

      So my text file consists of two lines of information:




      123456.99 :: working completed: result=0 , data ="{"day":"monday", "breakfast":"sandwich"}"
      123456.99 :: working completed: result=0 , data ="{"day":"tuesday", "breakfast":"bread"}"




      The first part of my code:



      fileID  = fopen('test1');
      text = textscan(fileID, '%s', 'delimiter','n','whitespace','');
      strLog = string(text{1});
      res = split(strLog, "data =");
      json_str = res(:, 2)


      And as a result I get a 2x1 string array. Output:




      json_str =



      2×1 string array



      ""{"day":"monday", "breakfast":"sandwich"}""
      ""{"day":"tuesday", "breakfast":"bread"}""



      This is where I got stuck.

      My first idea was to call cellfun and apply jsondecode.
      But I got




      Error using jsondecode JSON syntax error at line 1, column 4
      (character 4): extra text.




      But it makes no sence to me, since that should be the " from "day" which for json should be okay!?







      matlab






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 18:12









      RadoslavIvanovRadoslavIvanov

      35




      35
























          1 Answer
          1






          active

          oldest

          votes


















          0














          In json_str you have quote marks " at the start and end. These need to be removed for jsondecode to work. For example J = jsondecode(json_str{1}(2:end-1)).



          You can then use cellfun to process all elements. For example,



          S = cellfun(@(x)jsondecode(x(2:end-1)),json_str)





          share|improve this answer


























          • on the spot! is there a simple way to make it apply to all the lines? more general, in case my text file has 1000 lines not just 1 or 2

            – RadoslavIvanov
            Nov 14 '18 at 19:03











          • @RadoslavIvanov As you suggested you could use cellfun to process all elements of json_str

            – Azim
            Nov 14 '18 at 19:15











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306433%2fextra-text-when-using-jsondecode%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          In json_str you have quote marks " at the start and end. These need to be removed for jsondecode to work. For example J = jsondecode(json_str{1}(2:end-1)).



          You can then use cellfun to process all elements. For example,



          S = cellfun(@(x)jsondecode(x(2:end-1)),json_str)





          share|improve this answer


























          • on the spot! is there a simple way to make it apply to all the lines? more general, in case my text file has 1000 lines not just 1 or 2

            – RadoslavIvanov
            Nov 14 '18 at 19:03











          • @RadoslavIvanov As you suggested you could use cellfun to process all elements of json_str

            – Azim
            Nov 14 '18 at 19:15
















          0














          In json_str you have quote marks " at the start and end. These need to be removed for jsondecode to work. For example J = jsondecode(json_str{1}(2:end-1)).



          You can then use cellfun to process all elements. For example,



          S = cellfun(@(x)jsondecode(x(2:end-1)),json_str)





          share|improve this answer


























          • on the spot! is there a simple way to make it apply to all the lines? more general, in case my text file has 1000 lines not just 1 or 2

            – RadoslavIvanov
            Nov 14 '18 at 19:03











          • @RadoslavIvanov As you suggested you could use cellfun to process all elements of json_str

            – Azim
            Nov 14 '18 at 19:15














          0












          0








          0







          In json_str you have quote marks " at the start and end. These need to be removed for jsondecode to work. For example J = jsondecode(json_str{1}(2:end-1)).



          You can then use cellfun to process all elements. For example,



          S = cellfun(@(x)jsondecode(x(2:end-1)),json_str)





          share|improve this answer















          In json_str you have quote marks " at the start and end. These need to be removed for jsondecode to work. For example J = jsondecode(json_str{1}(2:end-1)).



          You can then use cellfun to process all elements. For example,



          S = cellfun(@(x)jsondecode(x(2:end-1)),json_str)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 19:16

























          answered Nov 14 '18 at 18:58









          AzimAzim

          6,50063257




          6,50063257













          • on the spot! is there a simple way to make it apply to all the lines? more general, in case my text file has 1000 lines not just 1 or 2

            – RadoslavIvanov
            Nov 14 '18 at 19:03











          • @RadoslavIvanov As you suggested you could use cellfun to process all elements of json_str

            – Azim
            Nov 14 '18 at 19:15



















          • on the spot! is there a simple way to make it apply to all the lines? more general, in case my text file has 1000 lines not just 1 or 2

            – RadoslavIvanov
            Nov 14 '18 at 19:03











          • @RadoslavIvanov As you suggested you could use cellfun to process all elements of json_str

            – Azim
            Nov 14 '18 at 19:15

















          on the spot! is there a simple way to make it apply to all the lines? more general, in case my text file has 1000 lines not just 1 or 2

          – RadoslavIvanov
          Nov 14 '18 at 19:03





          on the spot! is there a simple way to make it apply to all the lines? more general, in case my text file has 1000 lines not just 1 or 2

          – RadoslavIvanov
          Nov 14 '18 at 19:03













          @RadoslavIvanov As you suggested you could use cellfun to process all elements of json_str

          – Azim
          Nov 14 '18 at 19:15





          @RadoslavIvanov As you suggested you could use cellfun to process all elements of json_str

          – Azim
          Nov 14 '18 at 19:15




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306433%2fextra-text-when-using-jsondecode%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly