groovy - Jasper: how to find a specific item by value in a JSON array? - Stack Overflow

I'm designing a report with a JSON data source (groovy language).My JSON looks like this:{...&qu

I'm designing a report with a JSON data source (groovy language).

My JSON looks like this:

{
  ...
  "moreIds": [
    {
      "code": "OB4442735001",
      "codScheme": "CLIENT_REF"
    },
    {
      "code": "setr.010",
      "codScheme": "INITIAL_SWIFT_TYPE"
    },
    ...
  ],
  ...
}

I want to extract the code value for a given codScheme value (say "CLIENT_REF") from the moreIds array.

Initially I tried to extract the value in a field as follows:

<field name="orderClientRef" class="java.lang.String">
    <property name="net.sf.jasperreports.json.field.expression" value="moreIds.find{it.codScheme=='CLIENT_REF'}.code"/>
</field>

This doesn't work, it yields an empty value. I tried a bunch of other variations of the expression, but without success.

In the end this is what I did that worked:

I defined a field containing the full moreIds array:

<field name="moreIds" class="java.lang.Object">
    <property name="net.sf.jasperreports.json.field.expression" value="moreIds"/>
</field>

I used the following expression in the text field:

$F{moreIds}.find{it.get("codScheme").textValue()=="CLIENT_REF"}.get("code").textValue()

While it does achieve the goal, I find it less than elegant, and I'd like to know if there is a more elegant way to do this.

I'm designing a report with a JSON data source (groovy language).

My JSON looks like this:

{
  ...
  "moreIds": [
    {
      "code": "OB4442735001",
      "codScheme": "CLIENT_REF"
    },
    {
      "code": "setr.010",
      "codScheme": "INITIAL_SWIFT_TYPE"
    },
    ...
  ],
  ...
}

I want to extract the code value for a given codScheme value (say "CLIENT_REF") from the moreIds array.

Initially I tried to extract the value in a field as follows:

<field name="orderClientRef" class="java.lang.String">
    <property name="net.sf.jasperreports.json.field.expression" value="moreIds.find{it.codScheme=='CLIENT_REF'}.code"/>
</field>

This doesn't work, it yields an empty value. I tried a bunch of other variations of the expression, but without success.

In the end this is what I did that worked:

I defined a field containing the full moreIds array:

<field name="moreIds" class="java.lang.Object">
    <property name="net.sf.jasperreports.json.field.expression" value="moreIds"/>
</field>

I used the following expression in the text field:

$F{moreIds}.find{it.get("codScheme").textValue()=="CLIENT_REF"}.get("code").textValue()

While it does achieve the goal, I find it less than elegant, and I'd like to know if there is a more elegant way to do this.

Share Improve this question asked Mar 25 at 14:15 Olivier GérardinOlivier Gérardin 1,24516 silver badges31 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I suspect that net.sf.jasperreports.json.field.expression only wants a field name, and hence it's not happy with a full Groovy expression like that.

It seems like their JSON parser isn't the typical JsonSlurper from Groovy so your syntax you posted would have issues. However, you might simpilfy things (if it's a Groovy Expression) like so:

// working expression
$F{moreIds}.find{it.get("codScheme").textValue()=="CLIENT_REF"}.get("code").textValue()
// get is just the method name for [] operator so you could remove that using:
$F{moreIds).find(it["codScheme"].textValue()=="CLIENT_REF"}["code"]?.textValue()
// and dot notation could work if this is Groovy:
$F{moreIds).find(it.codScheme.textValue()=="CLIENT_REF"}?.code?.textValue()

I added the ?. operator so if things were null it wouldn't blow up and just gracefully return null.

I think you may look into JSONQL for what you are doing and that might be more elegant in one statement rather than selecting JSON and then applying Groovy Statement.

https://jasperreports.sourcefe/sample.reference/jsonqldatasource/README.html

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744190204a4562398.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信