nested JSON converting to java map problem

2,197 views
Skip to first unread message

80Vikram

unread,
Sep 16, 2016, 10:01:11 AM9/16/16
to REST assured
Hi,

I need to pass below to POST request

{
  "organization": "test",
  "context": {
    "description": "vikram-test"
  },
  "severity": "high",

}

I'm doing as below

        Map<String, Object>  jsonAsMap = new HashMap<>();
        jsonAsMap.put("organization", "test");
        jsonAsMap.put("severity", "high");
        jsonAsMap.put("context", new HashMap<String,String>() {{
                            put("description", "vikram-test");
                            }});


Is this correct way of passing JSON object ?

In my case context is not setting description value properly instead setting value as null

Please clarify

Thanks,
Vikram

Johan Haleby

unread,
Sep 16, 2016, 10:15:29 AM9/16/16
to rest-a...@googlegroups.com
Yes this is one way of doing it and it should work if you have jackson databind in the classpath. Note that you must also set the content-type header to application/json.

Regards,
/Johan

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

80Vikram

unread,
Sep 16, 2016, 10:26:08 AM9/16/16
to REST assured
I've below

   given().
                    header("Accept", "application/json; version=1.0").
                    contentType(ContentType.JSON).


and pom.xml entry

<dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
            <scope>test</scope>
        </dependency>


Is these sufficient to get it work ?

Thanks again.

Regards,
Vikram
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.

Johan Haleby

unread,
Sep 16, 2016, 10:42:01 AM9/16/16
to rest-a...@googlegroups.com
Regarding GSON I don't know off the top of my head 

To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.

80Vikram

unread,
Sep 21, 2016, 4:55:33 AM9/21/16
to REST assured
Hi Johan,

I could get this working by passing JSON document in the body

But it's failing when I pass as HashMap , is this a bug ?

Thanks & Regards,
Vikram

Johan Haleby

unread,
Sep 22, 2016, 1:07:33 PM9/22/16
to rest-a...@googlegroups.com
Most likely it's not a bug but a problem with your classpath. 
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Sent from my phone

Oleksandr

unread,
Mar 9, 2020, 7:04:15 AM3/9/20
to REST assured
try this one:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.10.3</version>
</dependency>
Reply all
Reply to author
Forward
0 new messages