Skip to content

Quotes in keys are not escaped #100

Open

Description

@ramyaprabhakar

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

Activity

  1. timhall commented on Jun 5, 2018

    @timhall
    Member

    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"}}
  2. ramyaprabhakar commented on Jun 6, 2018

    @ramyaprabhakar
    Author
  3. timhall commented on Jun 6, 2018

    @timhall
    Member

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

  4. reopened this on Jun 6, 2018
  5. changed the title [-]"Post" WebRequest with Inner Json that has double quotations and backslashes[/-] [+]Quotes in keys are not escaped[/+] on Jun 6, 2018
  6. ramyaprabhakar commented on Jun 6, 2018

    @ramyaprabhakar
    Author
  7. slishak commented on Jan 9, 2019

    @slishak

    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.

  8. slishak commented on Jan 10, 2019

    @slishak

    Seems to be fixed by #122

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions