Home » Java » java – How to get nested value from json (converted from XML) in ftl-Exceptionshub

java – How to get nested value from json (converted from XML) in ftl-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I was having some difficulties while accessing nested value from json in java.
Let me first tell my process to do it.

firstly I get input as XML file then I converted it to JSONObject
Here is my attempt to make HTML from FTL

HTMLMakerDemo

package com.imoveinvest.propertyuploader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.io.Writer;

import org.json.JSONObject;
import org.json.XML;
import org.springframework.stereotype.Service;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

@Service
public class PDFGenerationDemo implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static final String RESOURCES_DIR;
    public static final String OUTPUT_DIR;


    static {
        RESOURCES_DIR = "src//main//resources//";
        OUTPUT_DIR = "src//main//resources//output//";
    }

    public void makeHTML() throws IOException, TemplateException {
         String line = "", str = "";
         BufferedReader br = new BufferedReader(new FileReader(RESOURCES_DIR +
         "sample.xml"));
         while ((line = br.readLine()) != null) {
         str += line;
         }
         JSONObject root = XML.toJSONObject(str);
         JSONObject section = (JSONObject)root.get("property-valuation-report");
         section.put("data", section);
         PDFGenerationDemo pdf = new PDFGenerationDemo();
         pdf.makeTemplate(section);
         }
    public void makeTemplate(JSONObject obj) throws TemplateException{
        Configuration cfg = new Configuration();
        cfg.setObjectWrapper(new JSONArrayObjectWrapper());
        cfg.setClassForTemplateLoading(this.getClass(), "/templates/");


        try {
            Template sampleTemplate= cfg.getTemplate("sampleTemplate.ftl");
            Writer consoleWriter = new OutputStreamWriter(System.out);
            sampleTemplate.process(obj, consoleWriter);
            Writer fileWriter = new FileWriter(new File("output.html"));
            sampleTemplate.process(obj, fileWriter);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

My FTL is

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <body>
        <h1>Deserialization with Freemarker Hash</h1>
        <#assign obj = data.section>
        <#list obj as item>
            <#if item.name=="valuation">
                <div>${item.capital-value}</div>
            </#if>
        </#list>
    </body>
</html>

TO get rid of JSONparsing error I did this
from this https://stackoverflow.com/a/51876242/12876529

package com.imoveinvest.propertyuploader;

import org.json.JSONArray;
import org.json.JSONException;

import freemarker.template.DefaultObjectWrapper;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateSequenceModel;

@SuppressWarnings("deprecation")
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();
        }

    }


}

So my problem is in my XML file I have tags like
capital-value

<?xml version="1.0" encoding="utf-8"?>
<sample-data xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:ht="urn:hometrack.com/xslt/extensions">
    <section name="valuation">
        <capital-value>
            <from>855000</from>
            <to>1045000</to>
            <accuracy>medium</accuracy>
            <value>950000</value>
            <valuation-date>2020-02-12T00:00:00</valuation-date>
        </capital-value>
        <last-recorded-sale />
        <rental-value>
            <value>2720</value>
            <yield>3.4357894736842107</yield>
        </rental-value>
        <location>
            <lat>51.611573777232579</lat>
            <long>-0.32247146663868992</long>
        </location>
    </section>
</sample-data>

so when you look at my ftl you will see I have written

<div>${item.capital-value}</div>

which gives me error like

freemarker.core._TemplateModelException: An error has occurred when reading existing sub-variable "capital"; see cause exception! The type of the containing value was: extended_hash+string (org.json.JSONObject wrapped into f.e.b.StringModel)

----
FTL stack trace ("~" means nesting-related):
    - Failed at: ${item.capital - value}  [in template "sampleTemplate.ftl" at line 8, column 38]
----
    at freemarker.ext.beans.BeanModel.get(BeanModel.java:186)
    at freemarker.core.Dot._eval(Dot.java:43)
    at freemarker.core.Expression.eval(Expression.java:81)
    at freemarker.core.Expression.evalToNumber(Expression.java:118)
    at freemarker.core.ArithmeticExpression._eval(ArithmeticExpression.java:51)
    at freemarker.core.Expression.eval(Expression.java:81)
    at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:96)
    at freemarker.core.DollarVariable.accept(DollarVariable.java:59)
    at freemarker.core.Environment.visit(Environment.java:326)
    at freemarker.core.Environment.visit(Environment.java:368)
    at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:315)
    at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271)
    at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:242)
    at freemarker.core.Environment.visitIteratorBlock(Environment.java:595)
    at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:107)
    at freemarker.core.IteratorBlock.accept(IteratorBlock.java:93)
    at freemarker.core.Environment.visit(Environment.java:326)
    at freemarker.core.Environment.visit(Environment.java:332)
    at freemarker.core.Environment.process(Environment.java:305)
    at freemarker.template.Template.process(Template.java:378)
    at com.imoveinvest.propertyuploader.PDFGenerationDemo.makeTemplate(PDFGenerationDemo.java:57)
    at com.imoveinvest.propertyuploader.PDFGenerationDemo.makeHTML(PDFGenerationDemo.java:46)
    at com.imoveinvest.propertyuploader.controller.SaveOrValidateValuationReport.htmlCall(SaveOrValidateValuationReport.java:152)
    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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException: null
    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 freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:1487)
    at freemarker.ext.beans.BeanModel.invokeGenericGet(BeanModel.java:268)
    at freemarker.ext.beans.BeanModel.get(BeanModel.java:155)
    ... 64 common frames omitted
Caused by: org.json.JSONException: JSONObject["capital"] not found.
    at org.json.JSONObject.get(JSONObject.java:473)
    ... 71 common frames omitted
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <body>
        <h1>Deserialization with Freemarker Hash</h1>
                        <div>FreeMarker template error (DEBUG mode; use RETHROW in production!):

How to&Answers:

You will need to escape the hyphen in the property name ‘capital-value’

Only if you are using freemarker version from 2.3.22 or above. See freemarker variable syntax:

In this kind of expression, the variable name can only contain letters
(including non-Latin letters), digits (including non-Latin digits),
underline (_), dollar ($), at sign (@). Furthermore, the first
character can’t be a ASCII digit (0-9). Starting from FreeMarker
2.3.22, the variable name can also contain minus (-), dot (.), and colon (:) at any position, but these must be escaped with a preceding
backslash (), or else they would be interpreted as operators.