ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » JSON array using esql

Post new topic  Reply to topic
 JSON array using esql « View previous topic :: View next topic » 
Author Message
akashdwolf
PostPosted: Sun Sep 09, 2018 12:46 pm    Post subject: JSON array using esql Reply with quote

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
View user's profile Send private message
timber
PostPosted: Mon Sep 10, 2018 12:19 am    Post subject: Reply with quote

Grand Master

Joined: 25 Aug 2015
Posts: 1280

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
View user's profile Send private message
akashdwolf
PostPosted: Mon Sep 10, 2018 6:44 am    Post subject: Reply with quote

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
View user's profile Send private message
timber
PostPosted: Mon Sep 10, 2018 7:56 am    Post subject: Reply with quote

Grand Master

Joined: 25 Aug 2015
Posts: 1280

I see. Well that changes everything then. You are hitting a well known but very difficult problem. There is no single answer to it, and you will need to work out the best compromise solution for your customer/employer.

Please see this post first of all:
http://mqseries.net/phpBB/viewtopic.php?t=69068&sid=9d5f1a0cc73de1aa44e8dd8f73c411d3

This is the same problem...
http://mqseries.net/phpBB/viewtopic.php?t=72571&sid=0d11a73ed2e9fc0f5fdc5289f614e45f
I cannot find the code that automatically spots same-named elements that occur together. But it's not a reliable solution anyway, because of the XML-array-with-exactly-one-member scenario.
Back to top
View user's profile Send private message
akashdwolf
PostPosted: Mon Sep 10, 2018 9:26 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » JSON array using esql
Jump to:  



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
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.