|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
JSON array using esql |
« View previous topic :: View next topic » |
Author |
Message
|
akashdwolf |
Posted: Sun Sep 09, 2018 12:46 pm Post subject: JSON array using esql |
|
|
Apprentice
Joined: 09 Feb 2017 Posts: 28 Location: Mumbai
|
Below is DFDL:
<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:csv="http://www.ibm.com/dfdl/CommaSeparatedFormat" xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:ibmDfdlExtn="http://www.ibm.com/dfdl/extensions" xmlns:ibmSchExtn="http://www.ibm.com/schema/extensions">
<xsd:import namespace="http://www.ibm.com/dfdl/CommaSeparatedFormat" schemaLocation="IBMdefined/CommaSeparatedFormat.xsd"/>
<xsd:annotation>
<xsd:appinfo source="http://www.ogf.org/dfdl/">
<dfdl:format documentFinalTerminatorCanBeMissing="yes" encoding="{$dfdl:encoding}" escapeSchemeRef="csv:CSVEscapeScheme" ref="csv:CommaSeparatedFormat"/>
</xsd:appinfo>
</xsd:annotation>
<xsd:element ibmSchExtn:docRoot="true" name="a">
<xsd:complexType>
<xsd:sequence dfdl:outputNewLine="%CR;%LF;" dfdl:separator="" dfdl:terminator="">
<xsd:element dfdl:terminator="" maxOccurs="unbounded" name="Data">
<xsd:complexType>
<xsd:sequence dfdl:separator="" dfdl:terminator="">
<xsd:element dfdl:length="4" dfdl:lengthKind="explicit" name="value1" type="xsd:string"/>
<xsd:element dfdl:length="2" dfdl:lengthKind="explicit" name="value2" type="xsd:string"/>
<xsd:element dfdl:length="8" dfdl:lengthKind="explicit" name="value3" type="xsd:string"/>
<xsd:element dfdl:length="3" dfdl:lengthKind="explicit" name="value4" type="xsd:string"/>
<xsd:element dfdl:length="2" dfdl:lengthKind="explicit" name="value5" type="xsd:string"/>
<xsd:element dfdl:length="15" dfdl:lengthKind="explicit" name="value6" type="xsd:string"/>
<xsd:element dfdl:length="3" dfdl:lengthKind="explicit" dfdl:textNumberPattern="#0" name="value7" type="xsd:integer"/>
<xsd:element dfdl:length="4" dfdl:lengthKind="explicit" name="value8" type="xsd:string"/>
<xsd:element dfdl:length="1" dfdl:lengthKind="explicit" dfdl:terminator="" name="value9" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Belo is DFDL Input:
OIL L 0000019900000XXX-PUT-3626 0010000 ISWAX L 0000019900000XXX-PUT-3626 0020000 IS
Below is my code:
CREATE FIELD OutputRoot.JSON.Data.Request IDENTITY(JSON.Array);
DECLARE src REFERENCE TO InputRoot.DFDL.Record.Data ;
DECLARE tgt REFERENCE TO OutputRoot.JSON.Data.Request ;
WHILE LASTMOVE(src) DO
SET tgt.Item.Field1=src.value1 ;
SET tgt.Item.Field2=src.value2 ;
move src NEXTSIBLING ;
if LASTMOVE(src) THEN
CREATE NEXTSIBLING OF tgt AS tgt repeat ;
END IF ;
END WHILE;
Below is the current output:
{
"Request": [{
"Field1": "OIL",
"Field2": "L"
},
[{
"Field1": "WAX",
"Field2": "L"
}]
]
}
But below is the expected output:
{
"Request": [{
"Field1": "OIL",
"Field2": "L"
},
{
"Field1": "WAX",
"Field2": "L"
}
]
}
so basically I have to remove the extra bracket[] appended in 2nd record
Please help me
 |
|
Back to top |
|
 |
timber |
Posted: Mon Sep 10, 2018 12:19 am Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
I would add a Trace node and look carefully at the field types. I expect you are setting one of the OutputRoot elements to JSON.Array when you need JSON.Object.
If you want more help than that, please explain what you have tried - otherwise we will just tell you what you already know. |
|
Back to top |
|
 |
akashdwolf |
Posted: Mon Sep 10, 2018 6:44 am Post subject: |
|
|
Apprentice
Joined: 09 Feb 2017 Posts: 28 Location: Mumbai
|
Hi Timber,
Thanks for replying!!
Earlier I pasted DFDL details as well but you can ignore that I just wanted to give overview how I was getting the input.
Now coming to the point,My requirement is create a JSON array for multiple as well as single record.
Below is my code with which I am able to create a JSON array for multiple records But it is creating a normal JSON for a single record:
CREATE FIELD OutputRoot.JSON.Data.Request.Item IDENTITY(JSON.Array);
DECLARE src REFERENCE TO InputRoot.DFDL.a.Data ;
DECLARE tgt REFERENCE TO OutputRoot.JSON.Data.Request;
WHILE LASTMOVE(src) DO
SET tgt.Field1=src.value1 ;
SET tgt.Field2=src.value2 ;
move src NEXTSIBLING ;
if LASTMOVE(src) THEN
CREATE NEXTSIBLING OF tgt AS tgt repeat ;
ELSE
DELETE FIELD OutputRoot.JSON.Data.Request.Item ;
END IF ;
END WHILE;
Below is the output for a single record:
{
"Request":{
"Field1":"OIL",
"Field2":"L"
}
}
But I need it like below sample:
{
"Request":[
{
"Field1":"OIL",
"Field2":"L"
}
]
} |
|
Back to top |
|
 |
timber |
Posted: Mon Sep 10, 2018 7:56 am Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
|
Back to top |
|
 |
akashdwolf |
Posted: Mon Sep 10, 2018 9:26 am Post subject: |
|
|
Apprentice
Joined: 09 Feb 2017 Posts: 28 Location: Mumbai
|
Hi Timber,
Thanks for your help.
Below is the working code:
CREATE FIELD OutputRoot.JSON.Data.Request IDENTITY(JSON.Array);
DECLARE count INT CARDINALITY(InputRoot.DFDL.a.Data[]);
DECLARE i INT 1;
WHILE i <= count DO
SET OutputRoot.JSON.Data.Request.Item[i].Nam1 = InputRoot.DFDL.a.Data[i].value1 ;
SET OutputRoot.JSON.Data.Request.Item[i].Nam2 = InputRoot.DFDL.a.Data[i].value2 ;
SET OutputRoot.JSON.Data.Request.Item[i].Nam3 = InputRoot.DFDL.a.Data[i].value3 ;
SET i = i + 1;
END WHILE;
 |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|