0

I have the below xml response from one of the soap service:

<?xml version="1.0" encoding="UTF-8"?>
<idc:document DocUrl="//abc.com:443/images/ywnj/mdi5/~edisp/test.jpg" dDocFormats="image/jpeg" dDocName="Test1" dDocTitle="Test1.jpg" dID="603" dName="sysadmin" dStatus="RELEASED" dUser="anonymous" xmlns:idc="//www.stellent.com/IdcService/">
 <idc:field name="encodeDocUrl"/>
 <idc:field name="localizedForResponse">1</idc:field>
 <idc:field name="hasAnnotations">0</idc:field>
 <idc:field name="hasStickyNotes">0</idc:field>
 <idc:field name="dSubscriptionID">Test1</idc:field>
 <idc:field name="idcToken"/>
 <idc:resultset TotalRows="2" name="manifest">
  <idc:row>
   <idc:field name="extRenditionName">indexData</idc:field>
  </idc:row>
  <idc:row>
   <idc:field name="extRenditionName">48x48_GIF</idc:field>
  </idc:row>
  <idc:row>
   <idc:field name="extRenditionName">180x180_GIF</idc:field>
  </idc:row>
 </idc:resultset>
</idc:document> 
<idc:document DocUrl="//abc.com:443/images/ywnj/mdi5/~edisp/test23.jpg" dDocFormats="image/jpeg" dDocName="Test23" dDocTitle="Test23.jpg" dID="603" dName="sysadmin" dStatus="RELEASED" dUser="anonymous" xmlns:idc="//www.stellent.com/IdcService/">
 <idc:field name="encodeDocUrl"/>
 <idc:field name="localizedForResponse">1</idc:field>
 <idc:field name="hasAnnotations">0</idc:field>
 <idc:field name="hasStickyNotes">0</idc:field>
 <idc:field name="dSubscriptionID">Test23</idc:field>
 <idc:field name="idcToken"/>
 <idc:resultset TotalRows="2" name="manifest">
  <idc:row>
   <idc:field name="extRenditionName">indexData</idc:field>
  </idc:row>
  <idc:row>
   <idc:field name="extRenditionName">90x90_GIF</idc:field>
  </idc:row>
  <idc:row>
   <idc:field name="extRenditionName">250x250_GIF</idc:field>
  </idc:row>
 </idc:resultset>
</idc:document> 
  

and i have a below json response from another rest service:

{
  "results": [
    {
      "ID": "1",
      "ImageID": "2",
      "Name": "Test1",
      "Owner": "sysadmin",
      "Author": "sysadmin",
      "Creator": "sysadmin"
 },
 {
      "ID": "2",
      "ImageID": "23",
      "Name": "Test23",
      "Owner": "sysadmin",
      "Author": "sysadmin",
      "Creator": "sysadmin"
 }
  ]
}

and i want to achieve the final below json (the data from soap response should only be mapped if the xml dDocName matches the Name in the JSON.)

{
  "results": [
    {
      "ID": "1",
      "ImageID": "2",
      "Name": "Test1",
      "Owner": "sysadmin",
      "Author": "sysadmin",
      "Creator": "sysadmin"
   "links": [
                {
                    "rel": "indexData",
                    "href": "/abc/indexData"
                },
                {
                    "rel": "48x48_GIF",
                    "href": "/abc/48x48_GIF"
                },
    {
                    "rel": "180x180_GIF",
                    "href": "/abc/180x180_GIF"
                }
            ]
 },
 {
      "ID": "2",
      "ImageID": "23",
      "Name": "Test23",
      "Owner": "sysadmin",
      "Author": "sysadmin",
      "Creator": "sysadmin"
   "links": [
                {
                    "rel": "indexData",
                    "href": "/abc/indexData"
                },
                {
                    "rel": "90x90_GIF",
                    "href": "/abc/90x90_GIF"
                },
    {
                    "rel": "250x250_GIF",
                    "href": "/abc/250x250_GIF"
                }
            ]
 }
  ]
}

I tried creating the below in dataweave but its not working:

%dw 1.0
%output application/json
---
{
  "packshots": (payload.searchResults pluck ({
   "ID": $.xAH_StoreItemNumber,
   "ImageID": $.dID,
   "Name": $.dDocName,
   "Owner": $.dDocOwner,
   "Author": $.dDocAuthor,
   "Creator": $.dDocCreator,
    "links": (flowVars.renditionResp."idc:document"."idc:resultset"."idc:row" (filter $.dDocName == flowVars.CurrentName) map {
 "rel":$."idc:field".@extRenditionName,
 "href": "/abc" ++ $."idc:field".@extRenditionName
 })
 }))
 }

Could you please help me to achieve the same !!

Thanks !!

user3368821
  • 79
  • 2
  • 9
  • Looks like you're assuming both the XML and the JSON are `payload`, but they cannot be. You should use an enricher to surround one of the request and set the response to a `flowVar`. – jerney Aug 27 '18 at 18:17
  • Hello Jerney, sorry it was an type, the soap response indeed is in the flow variable and the another one is in payload. But its not working. I have corrected the snippet as well. – user3368821 Aug 28 '18 at 07:32
  • Sorry I didn't catch this the first time, but your XML document has 2 roots (`idc:document`), and it should only have one. Are you sure that's the correct response? – jerney Aug 28 '18 at 12:52
  • Yes jerney, this service is being invoked in a loop and the results are getting captured in an array list. After the loop i have a transformation where i want to achieve the above json !! – user3368821 Aug 29 '18 at 05:15

1 Answers1

0

I'm going to work under the assumption that your XML has a root tag called idc:root. What I would do first is group the XML by idc:dDocName. This will allow you to pull the correct part of the renditionResp variable when you need it.

Then, the rest of your script would look like this:

%dw 1.0
%output application/json

%var docs = flowVars.renditionResp."idc:root" groupBy $."idc:dDocName"
---
{
  "packshots": (payload.searchResults pluck ((value) -> {
    "ID":      value.xAH_StoreItemNumber,
    "ImageID": value.dID,
    "Name":    value.dDocName,
    "Owner":   value.dDocOwner,
    "Author":  value.dDocAuthor,
    "Creator": value.dDocCreator,
    "links":   docs[flowVars.CurrentName] map ((link) -> {
      "rel":  link."idc:field".@extRenditionName,
      "href": "/abc" ++ link."idc:field".@extRenditionName
    })
  })
}  
jerney
  • 2,187
  • 1
  • 19
  • 31