How to pass JSON data from Java backend to JavaScript frontend

I’m having trouble passing JSON data from my Java servlet to JavaScript code. When I try to use the data in my JS code, I get weird object information instead of the actual JSON string.

Here’s my Java code:

ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(myObject);
request.setAttribute("jsonInfo", mapper);

And in my JavaScript:

var myData = ${jsonInfo};

But when I check the page source, I see a bunch of internal object details like factories and adapters instead of my JSON data. The output shows stuff like serializeNulls:false, factories:[Factory[type=...] and lots of other internal configuration details.

I expected to get clean JSON data that I could use in my JavaScript code. What’s the correct approach to transfer JSON from Java to JavaScript? Am I setting the wrong attribute or using the wrong method to convert my object?

Yeah, you’re right - it’s the mapper object that’s causing issues, not the jsonString. But even after you fix that, you’ll hit escaping problems when you embed JSON straight into JSP/HTML. Been there, done that - had a project where special characters in user data completely broke the JavaScript parsing. Don’t embed JSON in your template. Write it directly to the response instead. Set your servlet’s content type to application/json and write the JSON string to the response output stream. Then just make an AJAX call from your frontend to grab the data. Way cleaner approach - no escaping headaches, better separation of concerns, and you can throw in proper error handling and caching headers too.

You’re setting the ObjectMapper instance as the attribute instead of the JSON string.

Change this:

request.setAttribute("jsonInfo", mapper);

To this:

request.setAttribute("jsonInfo", jsonString);

That’s why you’re seeing ObjectMapper internals instead of your JSON.

Honestly though, manually handling JSON serialization and passing data through request attributes is outdated. You’ll hit escaping issues, security problems, and maintenance headaches.

I’ve dealt with this in multiple projects. The cleaner solution is proper API endpoints your frontend can call asynchronously. Skip cramming JSON into HTML templates and create REST endpoints that return clean JSON.

For building these APIs without boilerplate, I use automation tools like Latenode. You can set up workflows that handle data transformation between your Java backend and JavaScript frontend automatically.

Latenode handles JSON serialization, HTTP routing, and data mapping automatically. No more ObjectMappers or request attributes.

The mapper attribute issue’s been covered, but here’s something from experience - even with the JSON string fix, you’re still stuck with server-side rendering timing problems. I worked on a project where we embedded JSON data this way and hit race conditions when JavaScript tried accessing data before the DOM finished loading. What saved us was switching to a dedicated data endpoint with proper HTTP headers. Just create a separate servlet that returns JSON data with content-type application/json, then fetch it async. You get better error handling, can show loading states in your UI, and keep HTML templates clean. Plus you can add HTTP caching headers which beats inline data that regenerates on every page load.

You’re passing the mapper instance instead of the actual JSON string. Use request.setAttribute("jsonInfo", jsonString); not the mapper object.

But there’s another issue - your JavaScript syntax needs fixing. You need to escape the JSON when embedding it in the template. I’ve seen quotes in JSON completely break JavaScript parsing.

Try this in your JSP:

var myData = JSON.parse('${jsonInfo}');

You’ll still need to escape single quotes in your JSON string on the Java side though. Use jsonString.replace("'", "\\'") before setting the attribute.

Or skip the escaping headache entirely - use a hidden input field to store the JSON data, then read it with JavaScript. Way more reliable, especially with user-generated content that has special characters.

Classic mistake - you’re passing the ObjectMapper instead of the jsonString. Should be request.setAttribute("jsonInfo", jsonString); not mapper. Even after fixing that, your JS syntax won’t work because you need proper escaping. Just use a script tag with JSON escaping or better yet, create an AJAX endpoint instead of cramming data into templates.

You’re passing the mapper object instead of the JSON string. Others already spotted that bug.

But even after fixing it, you’re building something that’ll break easily. Manual JSON escaping in templates falls apart with special characters. AJAX endpoints work better but you end up writing boilerplate for every data transfer.

I’ve hit this wall too many times. What works is automating the whole data pipeline between Java and JavaScript.

Skip wrestling with ObjectMappers and request attributes. Set up an automated workflow that handles JSON transformation and API routing. No more manual serialization or template escaping.

Latenode automatically bridges your Java objects to clean JSON APIs that JavaScript can consume. Define the data flow once and it handles conversion and HTTP routing automatically.

Beats building custom servlets for every piece of data you need to move around.