Created
April 20, 2025 20:53
-
-
Save halityurttas/eba3f68027880f49e77b080a39bc8212 to your computer and use it in GitHub Desktop.
Apisix json body modify sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local core = require("apisix.core") | |
| local plugin_name = "json-transformer" | |
| local schema = { | |
| type = "object", | |
| properties = { | |
| }, | |
| } | |
| local _M = { | |
| version = 0.1, | |
| priority = 0, | |
| name = plugin_name, | |
| schema = schema, | |
| } | |
| function _M.check_schema(conf) | |
| return core.schema.check(schema, conf) | |
| end | |
| function _M.access(conf, ctx) | |
| end | |
| function _M.header_filter(conf, ctx) | |
| end | |
| function _M.body_filter(conf, ctx) | |
| local headers = core.response.header(ctx) | |
| if not headers or not headers["Content-Type"] or | |
| not string.find(headers["Content-Type"]:lower(), "application/json") then | |
| return | |
| end | |
| local body = core.response.get_body() | |
| if not body then | |
| return | |
| end | |
| local data, err = core.json.decode(body) | |
| if not data then | |
| core.log.error("failed to decode JSON: ", err) | |
| return | |
| end | |
| if not data.results or not data.results[1] then | |
| return | |
| end | |
| local original = data.results[1] | |
| local transformed = { | |
| gender = original.gender, | |
| name = original.name, | |
| email = original.email, | |
| dob = original.dob, | |
| phone = original.phone, | |
| cell = original.cell | |
| } | |
| core.response.set_body(core.json.encode(transformed)) | |
| end | |
| return _M |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "results": [ | |
| { | |
| "gender": "male", | |
| "name": { | |
| "title": "Mr", | |
| "first": "Jacobo", | |
| "last": "Henríquez" | |
| }, | |
| "location": { | |
| "street": { | |
| "number": 4518, | |
| "name": "Callejón Urbina" | |
| }, | |
| "city": "Rioverde", | |
| "state": "Yucatan", | |
| "country": "Mexico", | |
| "postcode": 10096, | |
| "coordinates": { | |
| "latitude": "27.0536", | |
| "longitude": "-63.8377" | |
| }, | |
| "timezone": { | |
| "offset": "-1:00", | |
| "description": "Azores, Cape Verde Islands" | |
| } | |
| }, | |
| "email": "jacobo.henriquez@example.com", | |
| "login": { | |
| "uuid": "76674af0-f538-4a77-9adb-5e93ddb595f8", | |
| "username": "smallpanda412", | |
| "password": "mypass", | |
| "salt": "8toHQDD1", | |
| "md5": "c98f04cfd56f5cf37c119f6c8f9628bc", | |
| "sha1": "0ac9f954bc86a3b13fa33573b6bc84937c283391", | |
| "sha256": "142b6dc29542509e15d750432c2998c79d1379b58f4adb30490add4d460b44c4" | |
| }, | |
| "dob": { | |
| "date": "1996-06-26T08:51:56.913Z", | |
| "age": 28 | |
| }, | |
| "registered": { | |
| "date": "2013-08-13T13:28:44.780Z", | |
| "age": 11 | |
| }, | |
| "phone": "(688) 735 3956", | |
| "cell": "(676) 961 6792", | |
| "id": { | |
| "name": "NSS", | |
| "value": "98 81 54 1105 7" | |
| }, | |
| "picture": { | |
| "large": "https://randomuser.me/api/portraits/men/23.jpg", | |
| "medium": "https://randomuser.me/api/portraits/med/men/23.jpg", | |
| "thumbnail": "https://randomuser.me/api/portraits/thumb/men/23.jpg" | |
| }, | |
| "nat": "MX" | |
| } | |
| ], | |
| "info": { | |
| "seed": "a57ce5a533f345d0", | |
| "results": 1, | |
| "page": 1, | |
| "version": "1.4" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "gender": "male", | |
| "name": { | |
| "title": "Mr", | |
| "first": "Jacobo", | |
| "last": "Henríquez" | |
| }, | |
| "email": "jacobo.henriquez@example.com", | |
| "dob": { | |
| "date": "1996-06-26T08:51:56.913Z", | |
| "age": 28 | |
| }, | |
| "phone": "(688) 735 3956", | |
| "cell": "(676) 961 6792" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment