How to extract value from JSON response when using Spring MockMVC
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When testing a Spring MVC or Spring Boot controller with MockMvc, you usually need one of two things from the JSON response: quick inline assertions or the actual value for later use in the test. The best tool depends on which of those goals you have, and the usual choices are jsonPath, MvcResult, and Jackson deserialization.
Use jsonPath for Direct Assertions
If you only need to verify that a field exists and has the expected value, jsonPath is the cleanest option. It keeps the whole assertion next to the request.
This is ideal when the extracted value does not need to leave the assertion chain.
Useful patterns include:
- '
$.idfor a top-level field' - '
$.address.cityfor a nested field' - '
$[0].namefor the first item in a returned array'
For simple verification, this is usually better than manually parsing the response body.
Use MvcResult When You Need the Value in Java Code
If you need to store a value and use it later in the test, call andReturn() and read the response body.
This pattern is useful when a value from one response becomes input to a later request.
Deserialize the Whole Response for Typed Access
If the endpoint returns a structured object and you need several fields, parsing JSON into a DTO is often cleaner than reading many independent JSON paths.
This approach gives you compile-time field access and usually reads better when the response shape is non-trivial.
Example DTOs:
Reuse Extracted Values in Later Requests
This is a common testing pattern for create-then-fetch flows.
In this kind of test, extracting the raw value is more useful than just asserting it once.
Common Pitfalls
A common mistake is overusing getContentAsString() when jsonPath would have made the assertion shorter and clearer.
Another mistake is using jsonPath with the wrong expression syntax for arrays or nested objects. If an assertion fails unexpectedly, print the response body and verify the actual JSON structure first.
Developers also sometimes deserialize into the wrong DTO shape. If field names or nesting do not match the response payload, Jackson extraction fails even though the endpoint itself is fine.
Finally, remember that MockMvc gives you the raw response body as a string. If the response is not actually JSON, trying to read it with JSON tools will produce misleading test failures.
Summary
- Use
jsonPathfor concise inline assertions on JSON fields. - Use
MvcResultandgetContentAsString()when you need the raw response body. - Parse the JSON with JsonPath when you need one or two extracted values in Java code.
- Deserialize with Jackson when the response is better handled as a typed object.
- Choose the approach based on whether you need quick assertions or reusable extracted data.
Related reading
- How to fetch FetchType.LAZY associations with JPA and Hibernate in a Spring Controller
- How to find files that match a wildcard string in Java?
- How to find out the currently logged-in user in Spring Boot?
- How to find the index of an element in a TreeSet?
- How to get the path of src/test/resources directory in JUnit?
- How to handle a situation of feature scaling in machine learning model deployment when you have only one testing instance?
- How to find unused/dead code in java projects
- How to find/remove unused dependencies in Gradle

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.