2

I have the below JSON object hardcoded in my Java file

    JSONObject notificationInfoJson = new JSONObject();
    notificationInfoJson.put("title", "Payment Received");
    notificationInfoJson.put("firstName", "Bhuvan");
    notificationInfoJson.put("lastName", "Aggarwal");
    notificationInfoJson.put("accountId", "111");
    notificationInfoJson.put("paymentId", "555");

    JSONArray accounts = new JSONArray();
    for(int i=1; i<=2; i++) {
      JSONObject account = new JSONObject();
      account.put("accountId", 1000 + i);
      account.put("paymentId", 1000 + i);
      accounts.put(account);
   }    
    notificationInfoJson.put("accounts", accounts);

// passing the below json in the template as input
        JSONObject data = new JSONObject();
        data.put("notificationInfo", notificationInfoJson);

Json formed as:

{
  "title": "Payment Received",
  "firstName": "Bhuvan",
  "lastName": "Aggarwal",
  "accountId": "111",
  "paymentId": "555",
  "accounts": [
    {
      "accountId": 1001,
      "paymentId": 1001
    },
    {
      "accountId": 1002,
      "paymentId": 1002
    }
  ]
}

Can you please tell me how to read the Json array in FTL file?

FTL file:

<!doctype html>
<html lang="en">
    <head>
        <title>Notification</title>
    </head>
    <body>
        <div class="page">
            <p>Dear ${notificationInfo.firstName} ${notificationInfo.lastName}</p>
            <p>Your payment has been received for the following accounts</p>

            <#list notificationInfo.accounts?keys as account>
                <p>Account ID: ${account.accountId}</p>
                <p>Payment ID: ${account.paymentId}</p>
            </#list>

            <br>
            <p>Thank you!</p>
        </div>
    </body>
</html>

Error:

2018-08-16 16:59:29,929 [                          main] runtime                        ERROR Error executing FreeMarker template
FreeMarker template error:
For "." left-hand operand: Expected a hash, but this has evaluated to a string (wrapper: f.t.SimpleScalar):
==> account  [in template "payment-received.ftl" at line 12, column 34]

----
FTL stack trace ("~" means nesting-related):
    - Failed at: ${account.accountId}  [in template "payment-received.ftl" at line 12, column 32]
----

Java stack trace (for programmers):
----
freemarker.core.NonHashException: [... Exception message was already printed; see it above ...]
    at freemarker.core.Dot._eval(Dot.java:45)
    at freemarker.core.Expression.eval(Expression.java:78)
    at freemarker.core.Expression.evalAndCoerceToString(Expression.java:82)
    at freemarker.core.DollarVariable.accept(DollarVariable.java:41)
    at freemarker.core.Environment.visit(Environment.java:324)
    at freemarker.core.MixedContent.accept(MixedContent.java:54)
    at freemarker.core.Environment.visitByHiddingParent(Environment.java:345)
    at freemarker.core.IteratorBlock$IterationContext.executeNestedBlockInner(IteratorBlock.java:240)
    at freemarker.core.IteratorBlock$IterationContext.executeNestedBlock(IteratorBlock.java:220)
    at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:194)
    at freemarker.core.Environment.visitIteratorBlock(Environment.java:572)
    at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:78)
    at freemarker.core.IteratorBlock.accept(IteratorBlock.java:64)
    at freemarker.core.Environment.visit(Environment.java:324)
    at freemarker.core.MixedContent.accept(MixedContent.java:54)
    at freemarker.core.Environment.visit(Environment.java:324)
    at freemarker.core.Environment.process(Environment.java:302)
    at freemarker.template.Template.process(Template.java:325)
    at com.amdocs.bil.notification.beans.TemplateEngine.generateMessage(TemplateEngine.java:125)
    at com.amdocs.bil.notification.TestTemplateEngine.testTemplateEngine(TestTemplateEngine.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
2018-08-16 16:59:29,936 [                          main] TemplateEngine                 ERROR TemplateEngine.generateMessage: freemarker.core.NonHashException: For "." left-hand operand: Expected a hash, but this has evaluated to a string (wrapper: f.t.SimpleScalar):
==> account  [in template "payment-received.ftl" at line 12, column 34]

----
FTL stack trace ("~" means nesting-related):
    - Failed at: ${account.accountId}  [in template "payment-received.ftl" at line 12, column 32]
----
Bhuvan
  • 2,209
  • 11
  • 33
  • 46
  • `<#list notificationInfo.accounts?keys as account>` should be `<#list notificationInfo.accounts as account>`. Though even that only works if `notificationInfo.accounts` is a Java `Collection` (or other `Iterable`) or array or `Iterator`, and since `?keys` was successful it seems it's not. – ddekany Aug 16 '18 at 19:15
  • No it is not a Java collection. I thought adding "?keys" would work but didn't. Can you please suggest a better approach than the solution I provided? – Bhuvan Aug 17 '18 at 03:29
  • It's a bit of work (not much), but possible to make it work. See my answer. – ddekany Aug 17 '18 at 17:24
  • @ddekany: I am new to Freemarker. Can you post an example to show how to implement TemplateSequenceModel and do other things ? It will be helpful – Bhuvan Aug 22 '18 at 03:59

2 Answers2

3

I figured out that I have to use the below code in FTL file

notificationInfo.accounts will call JSONObject.get("accounts"), as FreeMarker knows the get(key) convention. The result of that is a JSONArray. But that doesn't implement List or anything familiar, so FreeMarker doesn't know how to list it.

Use the below code:

<#assign accounts = notificationInfo.accounts>
<#list 0 ..< accounts.length() as i>
   <p>Account ID: ${accounts.get(i).accountId} </p>
   <p>Payment ID: ${accounts.get(i).paymentId} </p>
</#list>

OR

Better way is to write your CustomObjectWrapper and set the ObjectWrapper in configuration. Thanks to @ddekany for suggestion

cfg.setObjectWrapper(new JSONArrayObjectWrapper());

    public class JSONArrayObjectWrapper extends DefaultObjectWrapper {

    @Override
    public TemplateModel handleUnknownType (Object obj) throws TemplateModelException {

        if (obj instanceof JSONArray) {
            return new JSONArraySequenceModel((JSONArray) obj);
        }

        return super.handleUnknownType(obj);
    }


    public class JSONArraySequenceModel implements TemplateSequenceModel {

        private JSONArray jsonArray;

        public JSONArraySequenceModel(JSONArray jsonArray) {
            this.jsonArray = jsonArray;
        }

        @Override
        public TemplateModel get(int index) throws TemplateModelException {
            TemplateModel model = null;
            try {

                model = wrap(jsonArray.get(index));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return model;
        }

        @Override
        public int size() throws TemplateModelException {
            return jsonArray.length();
        }

    }


}
Bhuvan
  • 2,209
  • 11
  • 33
  • 46
  • Which JSON API is this? Like what's the full qualified name of `JSONArray`? – ddekany Aug 16 '18 at 19:16
  • For future readers who cannot compile the JSONArraySequenceModel class. The wrap method in the following line: model = wrap(jsonArray.get(index)); is in the WrappingTemplateModel class. So JSONArraySequenceModel should extend WrappingTemplateModel: JSONArraySequenceModel extends WrappingTemplateModel implements TemplateSequenceModel – justadeveloper Jan 07 '22 at 10:05
  • @ddekany I believe the JSON API is the library at this link: https://github.com/stleary/JSON-java – justadeveloper Jan 07 '22 at 10:10
1

org.codehaus.jettison.json.JSONArray doesn't extend/implement any "standard" type (like java.util.List), so out-of-the-box it won't be seen as a listable value by FreeMarker. However, templates don't directly rely on java.util.List and such, but on TemplateModel-s. In FreeMarker there's an ObjectWrapper that wraps all objects into TemplateModel-s, and templates only see the wrapped objects. Thus, what's listable is decided by the ObjectWrapper. So, the steps of the solution are:

  1. Implement a TemplateSequenceModel that wraps (delegates to) a JSONArray. (Sometimes it's useful to also implement WrapperTemplateModel and AdapterTemplateModel, but it's optional.)

  2. Extend freemarker.template.DefaultObjectWrapper and override handleUnknownType, and if you get a JSONArray there then wrap it into your TemplateSequenceModel implementation. (See the handleUnknownType method of DefaultObjectWrapper itself as an example.)

  3. Set the object_wrapper setting of the configuration (for example via Configuration.setObjectWrapper) to an instance of your ObjectWrapper class.

Now <#list notificationInfo.accounts as account> should work.

ddekany
  • 29,656
  • 4
  • 57
  • 64
  • I am new to Freemarker. Can you post an example to show how to implement TemplateSequenceModel and do other things ? It will be helpful – Bhuvan Aug 22 '18 at 03:58