Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argument --parameter-values gives JSON parse error #102

Closed
aristotelos opened this issue Jan 8, 2020 · 14 comments
Closed

Argument --parameter-values gives JSON parse error #102

aristotelos opened this issue Jan 8, 2020 · 14 comments

Comments

@aristotelos
Copy link

aristotelos commented Jan 8, 2020

Running with the option --parameter-values as documented does not work, but gives a JSON parse exception:

PS C:\my-dir> npx angular-prerender --parameter-values '{":name":["amelia","oliver"]}'
Opties:
  --version           Toon versienummer                              [booleaans]
  --help              Toon help                                      [booleaans]
  --browser-target    specify the target inside your angular.json file which is
                      used to build the single page app
                                                   [string] [standaard: "build"]
  --config            specify the path to the angular.json file
                                                            [string] [standaard:
               "C:\mydir\angular.json"]
  --parameter-values  specify the parameter values which should be replaced with
                      the parameter in the routes     [string] [standaard: "{}"]
  --server-target     specify the target inside your angular.json file which is
                      used to build the server side code
                                                  [string] [standaard: "server"]
  --verbose, -v       set this flag if you prefer more detailed log messages
                                                  [booleaans] [standaard: false]

Unexpected token : in JSON at position 1

I tried other ways but did not yet succeed, except with an empty map: PS C:\my-dir> npx angular-prerender --parameter-values '{}'

@chrisguttandin
Copy link
Owner

Hi @aristotelos, thanks for bringing this to my attention. I can't reproduce the problem which is why I have the feeling that this could be an incompatibility between macOS and Windows.

Does it work if you use some other type of quotes?

@aristotelos
Copy link
Author

Well, PowerShell has single quotes as its standard escape character, so this seems the best option. I also tried with double quotes like this, but it fails:

PS C:\mydir> npx angular-prerender --parameter-values "'{`":name`":[`"amelia`",`"oliver`"]}'"
Opties:
  --version           Toon versienummer                              [booleaans]
  --help              Toon help                                      [booleaans]
  --browser-target    specify the target inside your angular.json file which is
                      used to build the single page app
                                                   [string] [standaard: "build"]
                                                            [string] [standaard:
               "C:\mydir\angular.json"]
  --parameter-values  specify the parameter values which should be replaced with
                      the parameter in the routes     [string] [standaard: "{}"]
  --server-target     specify the target inside your angular.json file which is
                      used to build the server side code
                                                  [string] [standaard: "server"]
  --verbose, -v       set this flag if you prefer more detailed log messages

Unexpected token : in JSON at position 1

Even simple JSON like this fails:

PS C:\mydir> npx angular-prerender --parameter-values '{"foo":"bar"}'
Opties:
  --version           Toon versienummer                              [booleaans]
  --help              Toon help                                      [booleaans]
  --browser-target    specify the target inside your angular.json file which is
                      used to build the single page app
                                                   [string] [standaard: "build"]
  --config            specify the path to the angular.json file
                                                            [string] [standaard:
               "C:\mydir\angular.json"]
  --parameter-values  specify the parameter values which should be replaced with
                      the parameter in the routes     [string] [standaard: "{}"]
  --server-target     specify the target inside your angular.json file which is
                      used to build the server side code
                                                  [string] [standaard: "server"]
  --verbose, -v       set this flag if you prefer more detailed log messages
                                                  [booleaans] [standaard: false]

Unexpected token f in JSON at position 1

@chrisguttandin
Copy link
Owner

Thanks for checking again. I believe now that all quotes somehow get lost. I don't know why though.

If you execute JSON.parse('{:name:[amelia,oliver]}') the error is 'SyntaxError: Unexpected token : in JSON at position 1' and if you execute JSON.parse('{foo:bar}') the error is 'Unexpected token f in JSON at position 1'. That matches with the error messages that you got.

I improved the error messages in v4.3. It will not help with the problem but it will help to verify my assumption because it logs the values as part of the error that could not be parsed.

Can you please check again with the latest version? Many thanks in advance.

@chrisguttandin
Copy link
Owner

Just a quick follow up. I found this documentation for the AWS CLI. It explains how to escape JSON values for PowerShell.

https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-parameters-quoting-strings.html

Does it work if you escape all quotes with a backslash and run: npx angular-prerender --parameter-values '{\":name\":[\"amelia\",\"oliver\"]}'?

@aristotelos
Copy link
Author

No, it doesn't work unfortunately. I'll show a couple of commands that I tried so far with their output.

PS>npx angular-prerender --parameter-values '{\":name\":[\"amelia\",\"oliver\"]}'
Please specify valid parameter values. The given value "{:name:[amelia,oliver]}" is invalid.
PS>npx angular-prerender --parameter-values '{":name":["amelia","oliver"]}'
Please specify valid parameter values. The given value "{:name:[amelia,oliver]}" is invalid.
PS>npx angular-prerender --parameter-values "{`":name`":[`"amelia`",`"oliver`"]}"
Please specify valid parameter values. The given value "{:name:[amelia,oliver]}" is invalid.
PS>npx angular-prerender --parameter-values "{':name':['amelia','oliver']}"
Please specify valid parameter values. The given value "{':name':['amelia','oliver']}" is invalid.
PS>npx angular-prerender --parameter-values "{\`"name\`":[\`"amelia\`",\`"oliver\`"]}"
Please specify valid parameter values. The given value "{name:[amelia,oliver]}" is invalid.
PS>npx angular-prerender --parameter-values '{`\"name`\":[`\"amelia`\",`\"oliver`\"]}'
Please specify valid parameter values. The given value "{`name`:[`amelia`,`oliver`]}" is invalid.
PS>npx angular-prerender --parameter-values '{\`"name\`":[\`"amelia\`",\`"oliver\`"]}'
Please specify valid parameter values. The given value "{\`name\`:[\`amelia\`,\`oliver\`]}" is invalid.

I even tried using cmd instead, but also with no success. It seems the " characters are simply removed.

CMD>npx angular-prerender --parameter-values '{\"name\":[\"amelia\",\"oliver\"]}'
Please specify valid parameter values. The given value "{name:[amelia,oliver]}" is invalid.

@chrisguttandin
Copy link
Owner

It seems like a hopeless attempt but the AWS docs say the cmd prompt should be encapsulated in double quotes. Maybe that's a combination worth trying.

npx angular-prerender --parameter-values "{\":name\":[\"amelia\",\"oliver\"]}"

Does that work?

@aristotelos
Copy link
Author

Tried that too, doesn't work:

CMD>npx angular-prerender --parameter-values "{\"name\":[\"amelia\",\"oliver\"]}"
Please specify valid parameter values. The given value "{name:[amelia,oliver]}" is invalid.

CMD>npx angular-prerender --parameter-values '{^"name^":[^"amelia^",^"oliver^"]}'
Please specify valid parameter values. The given value "{name:[amelia,oliver]}" is invalid.

@chrisguttandin
Copy link
Owner

To be honest I have no idea, what else you could try. But I was thinking about supporting files as well. Maybe that could be a solution which works for you as well. What would you think about something like this?

npx angular-prerender --parameter-values ./my-parameter-values.json

@aristotelos
Copy link
Author

I have found a way! Using an escaped backslash and escaped double quote actually works, in both PowerShell and cmd:

PS>npx angular-prerender --parameter-values '{\\\":name\\\":[\\\"amelia\\\",\\\"oliver\\\"]}'

It may have to do with this issue.

@aristotelos
Copy link
Author

The next question is: how do I escape a space inside a parameter value...

@chrisguttandin
Copy link
Owner

Wow thanks for the follow up. I will update the README once we resolved this issue.

Normally I would say don't escape whitespace at all, but I guess that doesn't work for you, right?

npx angular-prerender --parameter-values '{\\\":name\\\":[\\\"a m e l i a\\\",\\\"o l i v e r\\\"]}'

@aristotelos
Copy link
Author

Well, that doesn't work, it ends up with:

PS>npx angular-prerender --parameter-values '{\\\":name\\\":[\\\"a m e l i a\\\",\\\"o l i v e r\\\"]}'
Please specify valid parameter values. The given value "{":name":["a" is invalid.

I again tried one, two or three or four slashes, but that doesn't work... Because it seems to be a PowerShell or general npx issue I will close it here.

@aristotelos
Copy link
Author

I just found out what is a solution here: enclose the value in double quotes like this:

npx angular-prerender --parameter-values '\"{\\\":name\\\":[\\\"am e l i a\\\",\\\"o l i v e r\\\"]}\"'

@chrisguttandin
Copy link
Owner

Wow, I'm glad you found a solution. I added a small paragraph to the readme to mention that escaping might be an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants