Marce
Marce

Reputation: 103

Manipulate ArrayNode to be used as JsonBuilder parameter in Groovy

I'm trying to execute an Orchestration that retrieves a set of information from the server site and I want to manipulate the output in order to get only the necessary data.

The Output's manipulation menu, allow me to handle it via Groovy codification.

The output without manipulation throws a JSON like the following:

{
  "formId": "P43081_W43081A",
  "gridId": "1",
  "title": "Work With Orders Awaiting Approval",
  "rowset": [
    {
      "P43081_AN8_Originator": "304565",
      "P43081_DL01_Supplier": "I.T. Laptop/Tablets",
      "P43081_AN8_Supplier": "533104",
    },
    {
      "P43081_AN8_Originator": "304565",
      "P43081_DL01_Supplier": "I.T. Laptop/Tablets",
      "P43081_AN8_Supplier": "533104",
    },
    {
      "P43081_AN8_Originator": "304565",
      "P43081_DL01_Supplier": "Office Plus Supplies",
      "P43081_AN8_Supplier": "533103",
    }
  ],
  "records": 3,
  "moreRecords": false
}

I need only the information at rowset level. In order to do it, I manipulated the output including the following code:

import groovy.json.JsonSlurper;
import groovy.json.JsonBuilder;
import com.oracle.e1.common.OrchestrationAttributes;
String main(OrchestrationAttributes orchAttr, String input)
{
  def jsonIn = new JsonSlurper().parseText(input);
  // modify jsonIn;
  def jsonOut = new JsonBuilder(jsonIn.rowset);
  // orchAttr.writeWarn("custom log entry - warning");
  // orchAttr.writeDebug("custom log entry - debug");
  return jsonOut;
}

And this is throwing me the following error:

"message": "com.fasterxml.jackson.databind.node.ArrayNode cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode"

I deduce that the error cause is because I'm passing as JsonBuilder parameter an Array object (the rowset) instead of an Object.

Do you know how can I handle the output and pass the parameter in the correct format to JsonBuilder?

The output I want to get is:

{
  "P43081_AN8_Originator": "304565",
  "P43081_DL01_Supplier": "I.T. Laptop/Tablets",
  "P43081_AN8_Supplier": "533104",
},
{
  "P43081_AN8_Originator": "304565",
  "P43081_DL01_Supplier": "I.T. Laptop/Tablets",
  "P43081_AN8_Supplier": "533104",
},
{
  "P43081_AN8_Originator": "304565",
  "P43081_DL01_Supplier": "Office Plus Supplies",
  "P43081_AN8_Supplier": "533103",
}

Upvotes: 2

Views: 562

Answers (2)

Duane Duan
Duane Duan

Reputation: 13

This script is a Oracle JDE Orchestrator output define. the output must be a Map object. so you can modify like this:

  def jsonIn = new JsonSlurper().parseText(input); 
  def outMap = [:]; //define a map
  outMap.data = jsonIn.rowset; //set map={ "data":jsonIn.rowset}
  def jsonOut = new JsonBuilder(outMap ); 
  return jsonOut;

Upvotes: 0

cfrick
cfrick

Reputation: 37063

You can use JsonOutput instead. E.g.

import groovy.json.*

def data = new JsonSlurper().parse("data.json" as File)
println(JsonOutput.toJson(data.rowset));
// ⇒ [{"P43081_AN8_Originator":"304565","P43081_DL01_Supplier":"I.T. Laptop/Tablets","P43081_AN8_Supplier":"533104"},{"P43081_AN8_Originator":"304565","P43081_DL01_Supplier":"I.T. Laptop/Tablets","P43081_AN8_Supplier":"533104"},{"P43081_AN8_Originator":"304565","P43081_DL01_Supplier":"Office Plus Supplies","P43081_AN8_Supplier":"533103"}]

Upvotes: 1

Related Questions