json deserialized

88 views
Skip to first unread message

r0mm0n

unread,
Jan 17, 2022, 5:33:20 AM1/17/22
to KrakenD Community
Hello, it is possible to decompose json response with Krakend API gateway ?

For example:

I have response:
{"$type":"swagger_alfresco_dms_flow_search.k2RESTidentifier_ResultNode_DMS_FLOW[], swagger_alfresco_dms_flow_search, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null","$values":[{"$type":"swagger_alfresco_dms_flow_search.k2RESTidentifier_ResultNode_DMS_FLOW, swagger_alfresco_dms_flow_search, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null","version":"1","documentValid":"true","name":"MS TEAMS example1.docx","nodeId":"e64468d8-ea07-4a5b-ad1a-1115b372a3a0","title":"First doc","lastModifiedDate":"2021-10-07T22:11:27.87+02:00","createdDate":"2021-09-27T11:36:42.667+02:00","createdBy":"","comment":"This is an important document","mimeType":"application/vnd.openxmlformats-officedocument.wordprocessingml.document","mimeTypeName":"Microsoft Word 2007","path":"/Company Home/DMSFlow","lastVersion":true},{"$type":"swagger_alfresco_dms_flow_search.k2RESTidentifier_ResultNode_DMS_FLOW, swagger_alfresco_dms_flow_search, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null","documentSubTypeId":"1","version":"1","documentValid":"true","name":"automat.jpg","nodeId":"f4e240cd-3c54-4578-b221-45d1d8ee036d","documentTypeId":"1","title":"Second doc","documentStatusId":"1","lastModifiedDate":"2021-09-27T13:35:15.012+02:00","createdDate":"2021-09-27T11:39:56.973+02:00","createdBy":"","comment":"This is an important document","mimeType":"image/jpeg","mimeTypeName":"JPEG Image","path":"/Company Home/DMSFlow","lastVersion":false}]}


I need to extract part A or part B from the given response.

part A)
{"$type":"swagger_alfresco_dms_flow_search.k2RESTidentifier_ResultNode_DMS_FLOW, swagger_alfresco_dms_flow_search, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null","version":"1","documentValid":"true","name":"MS TEAMSexample1.docx","nodeId":"e64468d8-ea07-4a5b-ad1a-1115b372a3a0","title":"First doc","lastModifiedDate":"2021-10-07T22:11:27.87+02:00","createdDate":"2021-09-27T11:36:42.667+02:00","createdBy":"","comment":"This is an important document","mimeType":"application/vnd.openxmlformats-officedocument.wordprocessingml.document","mimeTypeName":"Microsoft Word 2007","path":"/Company Home/DMSFlow","lastVersion":true}

part B)
{"$type":"swagger_alfresco_dms_flow_search.k2RESTidentifier_ResultNode_DMS_FLOW, swagger_alfresco_dms_flow_search, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null","documentSubTypeId":"1","version":"1","documentValid":"true","name":"automat.jpg","nodeId":"f4e240cd-3c54-4578-b221-45d1d8ee036d","documentTypeId":"1","title":"Second doc","documentStatusId":"1","lastModifiedDate":"2021-09-27T13:35:15.012+02:00","createdDate":"2021-09-27T11:39:56.973+02:00","createdBy":"","comment":"This is an important document","mimeType":"image/jpeg","mimeTypeName":"JPEG Image","path":"/Company Home/DMSFlow","lastVersion":false}

Thank you for your response

R.

Albert Lombarte

unread,
Jan 17, 2022, 6:22:15 AM1/17/22
to KrakenD Community, r0mm0n
Hi there,

Yes, what you want to achieve is actually possible. The different data manipulation attributes allow you to pick the data you want to get from a response. Given that your response contains an array inside "$values" then you need to work with the flatmap filter (https://www.krakend.io/docs/backends/flatmap/).

Here there is a working configuration that you can execute to see how it works, it picks part A) but you could pick only part B as well. Note that since I don't have the Alfresco server, I have mocked the data inside the /alfresco endpoint. The flatmap takes the result you want and deletes the rest of the content. The "status" is deleted too because it comes from the mocking endpoint.

{
"version": 2,
"endpoints": [
{
"endpoint": "/alfresco",
"backend": [
{
"host": [
],
"url_pattern": "/__health"
}
],
"extra_config": {
"static": {
"strategy": "always",
"data": {
},
{
"endpoint": "/extraction",
"backend": {
"host": [
],
"url_pattern": "/alfresco",
"extra_config": {
"flatmap_filter": [
{
"type": "move",
"args": [
"$values.0",
"your_first_document"
]
},
{
"type": "del",
"args": [
"$values", "$type", "status"
]
}
]
}
}
}
}
]
}

El dia dilluns, 17 de gener de 2022 a les 11:33:20 UTC+1, r0mm0n va escriure:

r0mm0n

unread,
Feb 11, 2022, 3:47:52 AM2/11/22
to KrakenD Community, alb...@krakend.io, r0mm0n
Hi, it works absolutely great.
I still have a question about array processing. When I have multiple objects in the array and I want to use the move function for all members of a collection ?
example:

          "extra_config": {
            "github.com/devopsfaith/krakend/proxy": {
              "flatmap_filter": [
                {
                  "type": "move",
                  "args": [
                    "list.entries.0-99",
                    "move-all99"
                  ]
                }
              ]
            }
          },

Thank You.
Reply all
Reply to author
Forward
0 new messages