Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I set “output_encoding”: “json-collection”, the returned data is an empty array [] #930

Closed
Xuqy3 opened this issue Oct 8, 2024 · 2 comments
Labels

Comments

@Xuqy3
Copy link

Xuqy3 commented Oct 8, 2024

Environment info:

  • KrakenD version: devopsfaith/krakend:2.7.2
  • System info: docker
  • Backend technology: Java

Describe what are you trying to do:
When my backend api returns a data structure of

{
	"ret": 1,
	"data": [{
		"id": "f4376e0ab6454cbc8c865205bca7eff2"
	}]
}

I would like to return only the data for data to the user, i.e.

[{
	"id": "f4376e0ab6454cbc8c865205bca7eff2"
}]

When I set “target”: “data” and “output_encoding”: “json-collection”, krakend always returns an empty result with only “[]”, when I remove “target”: “data” it will When I remove the “target”: “data” and set "output_encoding": "json", it returns the full data, but I only want the “data” field.

I've compared to other api's and when the backend returns a data structure as

{
	"ret": 1,
	"data": {
		"total": 1,
		"id": "f87ce065898c40bb91b113bcb8748fd8"
	}
}

In this case “target”: “data” will extract the “data” correctly and return it to the user. The difference between them is that one of the structs of “data” is “{}”, while the other is a collection “[]”.

Your configuration file:

{
    "$schema": "https://www.krakend.io/schema/v3.json",
    "version": 3,
    "name": "KrakenD Gateway",
    "port": 8081,
    "timeout": "3000ms",
    "cache_ttl": "300s",
    "endpoints": [
        {
            "endpoint": "/tree",
            "method": "GET",
            "backend": [
                {
                    "url_pattern": "/tree",
                    "extra_config": {},
                    "encoding": "json",
                    "method": "GET",
                    "target": "data",
                    "host": [
                        "http://127.0.0.1:9998"
                    ]
                }
            ],
            "input_query_strings": [
                "*"
            ],
            "input_headers": [
                "*"
            ],
            "output_encoding": "json-collection"
        },
        {
            "endpoint": "/meetings",
            "output_encoding": "json",
            "method": "GET",
            "backend": [
                {
                    "url_pattern": "/meetings",
                    "extra_config": {},
                    "encoding": "json",
                    "sd": "static",
                    "method": "GET",
                    "target": "data",
                    "host": [
                        "http://127.0.0.1:9999"
                    ]
                }
            ],
            "input_query_strings": [
                "from",
                "to"
            ],
            "input_headers": [
                "*"
            ]
        }
    ]
}

Answers would be greatly appreciated.

@Xuqy3 Xuqy3 added the question label Oct 8, 2024
@alombarte
Copy link
Member

Hello @Xuqy3 ,

The json-collection encoding expects that the response from the backend is an object named collection that contains inside the array you want to return. Instead of doing a target, what you need is to rename the data property to collection and remove the rest of the noise. This is a working configuration you can try locally that does what you need:

{
    "version": 3,
    "$schema": "https://www.krakend.io/schema/krakend.json",
    "endpoints": [
        {
            "endpoint": "/collection",
            "output_encoding": "json-collection",
            "backend": [
                {
                    "url_pattern": "/data",
                    "host": [
                        "http://localhost:8080"
                    ],
                    "allow": [
                        "data"
                    ],
                    "mapping": {
                        "data": "collection"
                    }
                }
            ]
        },
        {
            "endpoint": "/data",
            "extra_config": {
                "proxy": {
                    "static": {
                        "strategy": "always",
                        "data": {
                            "ret": 1,
                            "data": [
                                {
                                    "id": "f4376e0ab6454cbc8c865205bca7eff2"
                                }
                            ]
                        }
                    }
                }
            },
            "backend": [
                {
                    "url_pattern": "/aaa"
                }
            ]
        }
    ]
}

@Xuqy3
Copy link
Author

Xuqy3 commented Oct 8, 2024

Thank you very much, I think my problem is solved!

@Xuqy3 Xuqy3 closed this as completed Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants