Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NextJS – Prevent NextResponse Adding Root Element to JSON Result

I have created an API in NextJs and the NextResponse adds a root element to the resulting JSON.

Here is the return in the api:

return NextResponse.json({result},{ status: 200 });

Here is the raw result from the call.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[{"id":11,"first_name":"test2","last_name":"employee","job_title":"test job","employeeStrengths":[{"id":65,"order":1,"strength":{"title":"Achiever"}},{"id":580,"order":2,"strength":{"title":"Ideation"}},{"id":581,"order":3,"strength":{"title":"Futuristic"}},{"id":582,"order":4,"strength":{"title":"Relator"}},{"id":583,"order":5,"strength":{"title":"Learner"}},{"id":66,"order":6,"strength":{"title":"Competition"}},{"id":579,"order":7,"strength":{"title":"Strategic"}},{"id":578,"order":8,"strength":{"title":"Analytical"}}]}]

Here is what the API (NextResponse) returns:

{
    "result": [
        {
            "id": 11,
            "first_name": "test2",
            "last_name": "employee",
            "job_title": "test job",
            "employeeStrengths": [
                {
                    "id": 65,
                    "order": 1,
                    "strength": {
                        "title": "Achiever"
                    }
                },
                {
                    "id": 580,
                    "order": 2,
                    "strength": {
                        "title": "Ideation"
                    }
                },
                {
                    "id": 581,
                    "order": 3,
                    "strength": {
                        "title": "Futuristic"
                    }
                },
                {
                    "id": 582,
                    "order": 4,
                    "strength": {
                        "title": "Relator"
                    }
                },
                {
                    "id": 583,
                    "order": 5,
                    "strength": {
                        "title": "Learner"
                    }
                },
                {
                    "id": 66,
                    "order": 6,
                    "strength": {
                        "title": "Competition"
                    }
                },
                {
                    "id": 579,
                    "order": 7,
                    "strength": {
                        "title": "Strategic"
                    }
                },
                {
                    "id": 578,
                    "order": 8,
                    "strength": {
                        "title": "Analytical"
                    }
                }
            ]
        }
    ]
}

I want to get rid of the root element "result: "

Any insight is appreciated.

>Solution :

Don’t destructor it.

return NextResponse.json(result,{ status: 200 });

One more thing, status: 200 is the default. You can git red of it

return NextResponse.json(result)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading