Search code examples
restnext.jshttpresponse

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.

[{"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)