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

Quotes in keys are not escaped #100

Open
ramyaprabhakar opened this issue Jun 5, 2018 · 6 comments
Open

Quotes in keys are not escaped #100

ramyaprabhakar opened this issue Jun 5, 2018 · 6 comments
Labels

Comments

@ramyaprabhakar
Copy link

Hi,
I am running into a problem I have no idea how to tackle.I need to construct a Json which looks like this to be passed as a request to get the sessionId as response:
{ "instruction":"login", "sessionId" : 0, "json": { ""user"":""xxxx"", ""password"":""yyyy"" } }

I'm currently passing the inner Json as Dictionary and collection parameters and then adding them to the request, but the request doesnt go in the above mentioned format whereas this is how its sent:
"{"instruction":"login","sessionId":0,"json":"[{"user":"xxxx"},{"password":"yyyy"}]"}"

My code:


Function QuerySpa() As WebResponse
Dim Client As New WebClient
Dim Request As New WebRequest
Client.BaseUrl = "http://localhost:8080/spa/api/login"

Dim c3 As Collection
Dim j3 As Dictionary
Dim j4 As Dictionary
Set c3 = New Collection
Set j3 = New Dictionary
Set j4 = New Dictionary

j3.Add "user", "xxxx"
j4.Add "password", "yyyy"
c3.Add j3
c3.Add j4

Request.Method = WebMethod.HttpPost
Request.ResponseFormat = WebFormat.JSON
'Request.Body = JsonString
Request.AddBodyParameter "instruction", "login"
Request.AddBodyParameter "sessionId", 0
Request.AddBodyParameter "json", JsonConverter.ConvertToJson(c3)

Set QuerySpa = Client.Execute(Request)

End Function


What is the best way to build the innerJson, If given as a string, then it is adding many //s and ""s
which are very difficult to handle. Your help would be much appreciated.

Thanks in Advance
Ramya

@timhall
Copy link
Member

timhall commented Jun 5, 2018

By calling ConvertToJson you are converting it into a string, which isn't what you want. Additionally, a Collection is converted into an array, which also isn't what you want.

With the following change you remove the quoted json value, which gets you closer:

Request.AddBodyParameter "json", c3

Debug.Print Request.Body
' {"instruction":"login","sessionId":0,"json":[{"user":"xxxx"},{"password":"yyyy"}]}

If you use a Dictionary instead of a Collection, you should get the format you are looking for:

Dim Json As New Dictionary
Json("user") = "xxxx"
Json("password") = "yyyy"

Request.AddBodyParameter "json", Json

Debug.Print Request.Body
' {"instruction":"login","sessionId":0,"json":{"user":"xxxx", "password":"yyyy"}}

@timhall timhall closed this as completed Jun 5, 2018
@ramyaprabhakar
Copy link
Author

ramyaprabhakar commented Jun 6, 2018 via email

@timhall
Copy link
Member

timhall commented Jun 6, 2018

Hmm, that may be a bug. I'll look into how keys are escaped and see what I can find.

@timhall timhall reopened this Jun 6, 2018
@timhall timhall changed the title "Post" WebRequest with Inner Json that has double quotations and backslashes Quotes in keys are not escaped Jun 6, 2018
@timhall timhall added the bug label Jun 6, 2018
@ramyaprabhakar
Copy link
Author

ramyaprabhakar commented Jun 6, 2018 via email

@slishak
Copy link

slishak commented Jan 9, 2019

I think there is still a problem with escape characters in object keys. The following subroutine illustrates the issue:

Sub BackslashTest()

    Dim json As New Dictionary
    
    json.Add "Test\Test", "Test\Test"
    Debug.Print JsonConverter.ConvertToJson(json)
    
    
End Sub

output:
{"Test\Test":"Test\\Test"}

So the backslash in the value is correctly escaped, but the backslash in the key is not.

@slishak
Copy link

slishak commented Jan 10, 2019

Seems to be fixed by #122

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

No branches or pull requests

3 participants