Search code examples
jsonyeomanyeoman-generator

parsing the string into the json in the method of yeoman generator


I am using yeoman generator to create a code with the template, where the data for the template is passed in the option as a string showed as below,

yo express-no-stress myapp --data {"title":"Type"}

this string data can be parsed as JSON later in the install methods.

  install() {
      async app() {
         let str = this.options.data;
         if(str != undefined && typeof str == 'string') {
              console.log(str);
              let field_property = JSON.parse(str);
         }
        .....
      } // parseapp 
  }// install 

however when running the application, it always encounters an error saying "syntaxError: Unexpected token t in JSON at position 1" in the JSON.parse() method.

I don't get why it causes this error, any idea, thanks.


Solution

  • You have to enclose your JSON in single quotes in the command line:

    yo express-no-stress myapp --data '{"title":"Type"}'
    

    Otherwise your shell tries to evaluate it.