Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6037149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:01:52+00:00 2026-05-23T06:01:52+00:00

We are building a restful api using Spring MVC and freemarker as the templating

  • 0

We are building a restful api using Spring MVC and freemarker as the templating language. We have chosen to build json responses in the freemarker

Example freemarker.ftl:

{
"field1" : "${response.value1}",
"field2" : "${response.value2}"
}

We get a problem when the strings in the values contain quotation marks (or any of the other characters in the JSON syntax).

The question: How can I escape these strings using freemarker?

We have looked at ?xml or ?html but they do not cover all relevant characters (such as \).

EDIT: ?js_string will escape the string to comform with JavaScript. And since JSON is based on JavaScript (JavaScript Object Notation), it will work.

EDIT2: In case a single-quote pops up, ?js_string will escape it which again leads to invalid JSON. The hotfix for it is:

${variable?js_string?replace("\\'", "\'")} 

and if you really want to be picky:

${variable?js_string?replace("\\'", "\'")?replace("\\>",">")}

Alternatively if you use Spring: http://www.springsurf.org/sites/1.0.0.M3/spring-webscripts/spring-webscripts-documentation/reference/html-single/index.html#js-api-index-org.springframework.extensions.webscripts.json.jsonutils

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:01:53+00:00Added an answer on May 23, 2026 at 6:01 am

    You’re looking for the ?js_string operator.

    {
    "field1" : "${response.value1?js_string}",
    "field2" : "${response.value2?js_string}"
    }
    

    That will take care of escaping quotes, backslashes, et. al in order to make your JS happy.

    Edit: I just saw that they introduced a ?json_string operator in Freemarker 2.3.19. See here for exactly how it works. And there was much rejoicing…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a RESTful web api with asp.net mvc, which returns pure json data.
When building a RESTful / hypermedia API with JSON resources, it seems I have
I am currently considering to build a single page web app using restful api
I'm building a data exchange server using a REST-like API. It's not strictly RESTful
I am building an RESTful API for an application I am working on and
I'm building a RESTful app on Django and Tasty Pie. I have a problem
I am building an spring restful project where i was applying spring security of
I'm building a RESTful API and want to provide developers with the option to
I'm building Pylons-based web application with RESTful API, which currently lacks any authentication. So
I'm building a RESTful web service, using Catalyst::Controller::REST . Usually for web testing I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.