How to assign a report field property value from JSON array element that has a particular value of one of its properties?

I am new to Jasper and I am trying to do something like in the snippet below to get "value" of the "node" from the "proposal.proposalFees.nodes" array with "node.type" equal "KEY_2".
Obviously, the below snippet doesn't work. Question: how to do it properly?
The goal is that "$F{KeyTwoValue}" value displays "56000" in expressions (see sample data below).

Any input would be greatly appreciated.

    <queryString language="json">
        <![CDATA[proposal]]>
    </queryString>
    <field name="KeyTwoValue" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression">
            <![CDATA[proposalFees.nodes[type == "KEY_2"].value]]>
        </property>
    </field>

Input source:

{
  "proposal": {
    "proposalFees": {
      "nodes": [
        {
          "value": 20000,
          "type": "KEY_1"
        },
        {
          "value": 56000,
          "type": "KEY_2"
        }
      ]
    }
  }
}

anatoly.weinstein's picture
Joined: Apr 21 2023 - 1:52am
Last seen: 4 months 3 weeks ago

Actually, I have found an answer to my question here: jsonql datasource reference

This page has a good comparison for syntax and capability of JSON and JSONQL.

In question (with language="json") the proper field definition will looks like this:

    <field name="KeyTwoValue" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression"
                  value="proposalFees.nodes(type == KEY_2).value[0]"/>
    </field>

anatoly.weinstein - 4 months 4 weeks ago

We are delighted to know that you have found the answer to your question. Thank you for taking the time to post it on the Jaspersoft Community.

mrajkuma - 4 months 4 weeks ago

1 Answer:

jsonql datasource reference

This page has a good comparison for syntax and capability of JSON and JSONQL.

In question (with language="json") the proper field definition will looks like this:

    <field name="KeyTwoValue" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression"
                  value="proposalFees.nodes(type == KEY_2).value[0]"/>
    </field>
mrajkuma's picture
Joined: Jan 27 2023 - 2:28am
Last seen: 6 days 23 hours ago
Feedback