Best way to load some JSON files into a Spring Boot application
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The best way to load JSON files in a Spring Boot application depends on what those files represent. If they are application configuration, bind them into typed properties. If they are business data, fixtures, or seed content, load them as resources and deserialize them with Jackson. The mistake is treating every JSON file like generic text instead of deciding whether it belongs to configuration, startup data, or request-time input.
Choose the Loading Strategy by Use Case
These are the most common cases:
- configuration-like values used during startup
- static JSON resources bundled inside the application
- external JSON files mounted or supplied at runtime
Once you classify the file, the loading pattern becomes much clearer.
Loading Classpath JSON With Resource
For bundled JSON files under src/main/resources, Spring's Resource abstraction plus Jackson is usually the cleanest approach.
Assume a file at src/main/resources/data/countries.json:
Java model:
Loader service:
This is a good default because it is typed, testable, and works the same from the IDE and from the packaged jar.
Loading Data at Startup
If the JSON should be read once on application startup, use ApplicationRunner or CommandLineRunner rather than scattering file reads across controllers.
This makes startup behavior explicit and keeps file loading out of request paths unless you truly need dynamic reload behavior.
When the JSON Is External
If operations teams need to replace the file without rebuilding the jar, do not keep it only on the classpath. Point to an external location through configuration:
Then inject the path:
This is more flexible for deployment-specific data.
Avoid Treating JSON as a Generic String Blob
A weak pattern is to read the file into a String and pass raw JSON around the application. That loses type safety and pushes parsing errors further downstream. Deserialize at the boundary instead and keep the rest of the code working with normal Java objects.
If the file contents are truly dynamic and schema-free, use JsonNode rather than raw strings so you still get structured access.
Common Pitfalls
- Using
application.propertiesorapplication.yamlto embed large business-data JSON blobs. - Reading classpath files with plain filesystem paths, which often breaks once the app is packaged.
- Parsing JSON late and passing raw text through service layers.
- Loading reference files on every request instead of at startup or through a cache.
- Choosing classpath storage when operations actually need runtime-replaceable external files.
Summary
- Pick the loading strategy based on whether the JSON is configuration, bundled data, or external runtime data.
- For bundled files, use Spring
ResourceAPIs and Jackson deserialization. - For startup-only loading, centralize the read in an
ApplicationRunneror similar boot hook. - For runtime-managed files, inject an external path through configuration.
- Deserialize into typed objects early so the rest of the application stays simple.

