Question

Problem with passing json with nested arrays

  • 11 March 2022
  • 3 replies
  • 2116 views

Hi,

This is not a new issue from what I can see, but solutions that already are on this forum didn’t solve my issue or maybe I’m doing something wrong.

What I’m trying to do is to pass json file from one endpoint to another. To do this I’m using a Webhook where external application is passing json then another Webhook should pass modified json. The problem that I have is with the array filed. When I create data I have String fields. Fields are combined as one.

I tried to use Code with JavaScript as was showed on the forum but I still get at the end appended string not arrays. 

I tried both : Solution 1 and Solution 2 but with no positive outcome.

This is my row body I get with my webhook:

{"dataObject":{"general":{"o_classId":"1","o_className":"Product","o_published":true,"o_id":6,"o_parentId":5,"o_type":"object","o_key":"Led Bulb","o_creationDate":1646831088,"o_userOwner":2},"data":{"category":[{"dest_id":"7","id":"7","type":"object","subtype":"Category","published":true,"path":"\/Categories\/Light","index":"1","fullpath":"\/Categories\/Light","classname":"Category","rowId":"7$$1$$object"},{"dest_id":"3","id":"3","type":"object","subtype":"Category","published":true,"path":"\/Categories\/Electro","index":"2","fullpath":"\/Categories\/Electro","classname":"Category","rowId":"3$$2$$object"}],"color":"white","image":{"type":"image","id":5,"parentId":3,"parent":null,"filename":"ledbulb.jpg","path":"\/Lightning images\/","mimetype":"image\/jpeg","creationDate":1646830813,"modificationDate":1646834413,"stream":null,"userOwner":2,"userModification":0,"properties":null,"versions":null,"metadata":[],"locked":null,"customSettings":{"imageDimensionsCalculated":true,"imageWidth":640,"imageHeight":480,"embeddedMetaData":{"FileName":"ledbulb.jpg","FileDateTime":1646830813,"FileSize":32027,"FileType":2,"MimeType":"image\/jpeg","SectionsFound":""},"embeddedMetaDataExtracted":true},"hasMetaData":false,"siblings":null,"hasSiblings":null,"dataChanged":false,"versionCount":1,"dependencies":null,"__dataVersionTimestamp":1646834413,"dao":null,"_fulldump":false,"o_dirtyFields":null,"scheduledTasks":null,"____pimcore_cache_item__":"asset_5"},"name":"Led Bulb"},"metaData":{"category":{"objectid":6,"inherited":false},"color":{"objectid":6,"inherited":false},"image":{"objectid":6,"inherited":false},"name":{"objectid":6,"inherited":false}}},"arguments":[]}
 
Then in Code I go with simple script

var obj = JSON.parse(inputData.rawjson);

return obj;

 
And in test I get nice field:

 

 

but in webhook that is preparing json to be sent I have still those :

 

Is there maybe something I’m missing in the flow? My task is rather simple and standard so there should not be any problems with this. Please help :)
 
 
 
 
 

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

3 replies

Userlevel 7
Badge +11

Hey there @HyKamil!

I‘m not 100% certain from what I’m seeing in the screenshots but, it looks like the Code step is outputting the category Paths as line items rather than individual fields for each category. So it seems like you’ve got at least two line items that are being output in the selected Data Object Data Category Path field (category 7 and 3).

I’m guessing that you’re looking to select the categories individually as separate fields, is that correct?

If so, what you could try here is to add a Formatter (Utilities > Line-item to Text) step directly after the Code step. That would convert the line items to text and output all line item elements into separate fields. You can find out more about converting line items here: Convert line items into text strings


Hopefully that helps! :slight_smile:

Hi,

You are correct about what I’m after :).

I want to get json like this

{

“data”:{

 “category”:[

  { “id”: “7”},

  { “id”: “3”}

 ]}

}

but what I get  is:

{

“data”:{

 “category”{

“id” : “7, 3”

 } }

}

 

I tried what you suggested but I cannot add 4th step with formatter. I get error:

 

Ok, I created new flow and was able to add 4th and 5th step but it not solved the issue entirely.

Let me start again with the issue as maybe there is a simple way to do this.

My flow is rather simple: receive JSON → map it → send new JSON.

I started with 2 steps

  1. Trigger Webhook ->Catch Hook
  2. Action Webhook → POST

Images below are my test in first step (looks ok) than mapping in second step. I can already see at this point that recognition of field is wrong for Category ID. I don’t have any issues with filed like dataObject.general.o_key or dataObject.data.color, but when I want  to get dataObject.data.category.id the problem starts.

 

 

 

 

Then if I proceed what I will get on ma endpoint  JSON in left column, instead of what I want in right column ( I manually eddied right column)

{
"object": {
"data": {
"category": {
"id": "3,7",
"type": "/Categories/Electro,/Categories/Light"
},
"color": "white"
},
"name": "Led Bulb"
}
}

 

{
"object": {
"data": {
"category": [
{
"id": "3",
"type": "/Categories/Electro"
},
{
"id": "7",
"type": "/Categories/Light"
}
]
},
"color": "white"
},
"name": "Led Bulb"
}

 

 

So another flow that I created is based on solutions mention before, on this forum plus @SamB suggestion.

New flow looks like this:

This is what I have in me hook on the test:

 

then JavaScript

code:

var obj = {},
jsondata = JSON.parse(inputData.rawjson);
for(var y in jsondata.dataObject.data.category[0]){

obj[y] = [];
}
for(var x in jsondata.dataObject.data.category){
for(var y in jsondata.dataObject.data.category[x]){
obj[y].push(jsondata.dataObject.data.category[x][y]);
}
}
obj.webhook = jsondata;
return obj;

 

Then I add two formatters for two fields Category Id and Category Path. I understand that one filed - one Formatter.

So after test in one step I have this:

Last step is Web Hook with POST method and mapping all the things.

However I still cannot or don't know how to  create array in JSON. I cannot give the same name and best I can do is JSON bellow. Still category is not a list but fields are separated.

Map:

and final JSON

{
"object": {
"data": {
"category": {
"id": "3",
"id1": "7",
"type": "/Categories/Light",
"type1": "/Categories/Electro"
},
"color": "white"
},
"name": "Led Bulb"
}
}

 

Any ideas ?