Botframework-solutions: SyntaxError: Unexpected token \ in JSON at position 1

Created on 26 Oct 2018  路  11Comments  路  Source: microsoft/botframework-solutions

Description
Hi, I'm trying to run the following, I've changed the variables _YOUR_AUTH_CONNECTION_NAME_, _YOURBOTFILE_ and _YOUR_BOT_SECRET_ when I run it.

msbot connect generic --name "Authentication" --keys "{\"Azure Active Directory v2\":\"YOUR_AUTH_CONNECTION_NAME\"}" --bot YOURBOTFILE.bot --secret "YOUR_BOT_SECRET" --url "portal.azure.net"

I get the following error:

SyntaxError: Unexpected token \ in JSON at position 1

To Reproduce
Steps to reproduce the behavior:

  1. Using PowerShell 5.1 shell and Windows 10.
  2. Run the command with variables changed as required.
  3. Syntax error will appear.
Bug Docs

All 11 comments

Looks like it works when running it from the Windows Azure SDK Environment CLI instead of PowerShell.

Thanks for raising, I've tried a number of examples but it seems that PowerShell is rejecting the JSON as the "key" (Azure Active Directory v2) has spaces in. Also the outer " around the json should be '

So it accepts this (but this won't work in the Bot file as the key name is wrong!)

msbot connect generic --name "Authentication" --keys '{\"AzureActiveDirectoryv2\":\"YOUR_AUTH_CONNECTION_NAME\"}' --bot va-dj-us-en.bot --url "portal.azure.net"

I look into this further and see if there is a workaround, I've tried using Convert-FromJson and so on but no joy :( e.g.:

$json = @"
{
    "Azure Active Directory v2":"YOUR_AUTH_CONNECTION_NAME"
}
"@

$jsonObject = $json | ConvertFrom-Json

Your workaround was _almost_ there. You have to convert it back to a string. That does work. It seems as though it wants some linefeeds/carriage returns to the string. So basically, converting to a PSCustomObject then back to a JSON string. Additionally, the escape character \ is not needed. Here's what I tried:

$keysstring = '{"Azure Active Directory v2":"YOUR_AUTH_CONNECTION_NAME"}'
$keysobject = ConvertFrom-Json $keysstring
$keys = ConvertTo-Json -InputObject $keysobject

At this point $keys will output:

{
    "Azure Active Directory v2":  "YOUR_AUTH_CONNECTION_NAME"
}

Then you can run:

msbot connect generic --name "Authentication" --keys $keys --bot BotConfiguration.bot --secret "YOUR_BOT_SECRET" --url "portal.azure.net"

Alternatively, you can construct the $keys variable string on multiple lines beforehand (not good for scripting of course)

$keys = '{
>> "Azure Active Directory v2":"YOUR_AUTH_CONNECTION_NAME"
>> }'

The above workaround should be your solution. Unfortunately there are no more elegant ways of doing this at this time. I am closing the issue since this should unblock you. Feel free to reopen if this does not quite solve your scenario.

Closing since @dmvtech proposed a workaround and there seems to be no nicer solution. Reopen if you want to discuss further.

The above workarounds aren't working for me on msbot v4.3.0

Which error do you get:

SyntaxError: Unexpected token A in JSON at position 1

or

Error: mising --url (even when supplying --url)

The unexpected token error.

This workaround didn't work for me. After messing with this for a few hours I've found the working solution:

msbot connect generic --bot MyBot.bot --url https://westeurope.api.cognitive.microsoft.com --keys '{\"subscriptionKey\":\"some_key\"}' --name Name

Json should be wrapped in '' and all properties should have "" properly escaped with \ and no spaces in JSON if used from cmd

Esta soluci贸n no funcion贸 para m铆. Despu茅s de jugar con esto por unas horas, encontr茅 la soluci贸n de trabajo:

msbot connect generic --bot MyBot.bot --url https://westeurope.api.cognitive.microsoft.com --keys '{\"subscriptionKey\":\"some_key\"}' --name Name

Json debe estar envuelto ''y todas las propiedades deben haberse ""escapado correctamente \y no deben haber espacios en JSON si se usan desde cmd

Excuse my ignorance but subscribeKey = IdSuscription but in \ some_key \ where do you get it from?
name is equal to subscription name?

Excuse my ignorance but subscribeKey = IdSuscription but in \ some_key \ where do you get it from?
name is equal to subscription name?

That's very good question. I have not been working with bots since January and don't remember exactly. But as far as I remember, subscriptionKey is property name, and stays like this. You don't change it to the value, it's property name. and some_key is your subscription key.

In the end you should get smth like:
--keys '{\"subscriptionKey\":\"1cjsdkdjf34jdfskdjf83948jdfskjf9w3859jdfksfj\"}'

Was this page helpful?
0 / 5 - 0 ratings