lars1595
lars1595

Reputation: 925

How to create a complex object in ESQL

I would like to create the following JSON object in ESQL and put it on the SET OutputRoot.JSON.Data. How do I do that?

{ 
      "active"   : [ {"name" : "test"}, 
                     {"name": "test2"}]
      "inactive" : [ {"name" : "test3"}]
}

Upvotes: 0

Views: 4103

Answers (2)

Igor Murzich
Igor Murzich

Reputation: 51

The below snippet demonstrates how to create JSON you need:

    CREATE FUNCTION Main() RETURNS BOOLEAN
    BEGIN
        -- Create JSON domain
        CREATE LASTCHILD OF OutputRoot DOMAIN 'JSON' NAME 'JSON';
        -- Create root Data field
        CREATE FIELD OutputRoot.JSON.Data;
        DECLARE OutMsg REFERENCE TO OutputRoot.JSON.Data;

        CREATE FIELD OutMsg.active IDENTITY (JSON.Array)active;
        SET OutMsg.active.Item[1].name = 'test';
        SET OutMsg.active.Item[2].name = 'test2';
        SET OutMsg.(JSON.Array)inactive.Item[1].name = 'test3';
        RETURN TRUE;
    END;

Also you can read more information about work with JSON here Manipulating messages in the JSON domain and here Creating or transforming a JSON message by using a message map

Upvotes: 1

kimbert
kimbert

Reputation: 2422

There is a standard procedure for IIB developers who want to output a specific format of XML/JSON:

  1. Use a text editor to create the document that you want to output
  2. Create a simple message flow that parses that document.
    • On the FileInput node (or HTTPInput, if you prefer) set the Domain to 'JSON'
    • Make sure that the second node is a Trace node with Pattern set to '${Root}'.
  3. Put the example JSON through the message flow
  4. Examine the Trace node output, paying special attention to the field type on each node
  5. Write ESQL that produces an identical message tree under OutputRoot.JSON.Data

Upvotes: 2

Related Questions