Parse JSON string from sub-worker

Any recommendations from this awesome community? I am not able to JSON.parse a response from a sub-worker, but if I use the response as a direct string to test, it works. I confirmed the returned json is valid in a JSON lint tester online.

Error message:

"MSG: \"[object Object]\" is not valid JSON, Stack:SyntaxError: \"[object Object]\" is not valid JSON\n at JSON.parse ()\n at d1-beta-facade.entry.js:1306:21\n at async d1-beta-facade.entry.js:1270:7\n at async d1-beta-facade.entry.js:1210:50"

Worker I am calling to validate a JWT token which returns a JSON.stringified object:

import jwt from '@tsndr/cloudflare-worker-jwt'

export default {
    async fetch(request, env) {
        const tokenField = request.headers.get('authorization')

        // Remove the "bearer" label
        const token = tokenField.substring(6).trim();

        try {
            // NOTE: Validate also checks the timestamp TTL 
            const isValid = await jwt.verify(token, env.JWT_TOKEN_KEY, { algorithm: 'HS256' })
            if (!isValid) {
                return new Response('Invalid Java Web Token (JWT) 1.', { status: 403 });
            else {
                const { payload } = jwt.decode(token)
                // return new Response(JSON.stringify(payload), { status: 200 })

                const json = JSON.stringify(payload, null, 2);
                return new Response(json, {
                    headers: {
                        'content-type': 'application/json;charset=UTF-8',

        } catch (e) {
            return new Response('Invalid Java Web Token (JWT) 2.', { status: 403 });

The worker calling the above sub-worker:

import { Hono } from 'hono';
import { cors } from 'hono/cors';

const app = new Hono()
app.use('/*', cors());

interface UserJwt {
    sub: string;
    name: string;
    email: string;
    roles: string;
    exp: number;
    iat: number;
}'/', async c => {
    try {

  // This fails
        const results = await c.env.authsvc.fetch(c.req.clone())
        // It returns this: '{"sub":"[email protected]","name":"[email protected]","email":"[email protected]","roles":"user tester","exp":1672242676,"iat":1672235476}'
        let user: UserJwt = JSON.parse(results);
        return c.json(;

 // This works
        // let user: UserJwt = JSON.parse('{"sub":"[email protected]","name":"[email protected]","email":"[email protected]","roles":"user tester","exp":1672242676,"iat":1672235476}');
       // return c.json(;

    } catch (exception: any) {
        return c.json("MSG: " + exception.message + ", Stack:" + exception.stack);

export default app;

Thanks for any pointers.

results is a Response object.

If you want the response body, just do await results.json().

Thanks for your help. I used this code and still had the same error message:'/', async c => {
     const results = await c.env.authsvc.fetch(c.req.clone())
     const user = JSON.parse(results.json());
     return c.json(;

There is no need for JSON.parse when using .json()

Ahh, gotcha. Now the response is undefined for the returned object’s name field. I am switching from C# to TypeScript so still learning. Thank you so much for the help.

        const results = await c.env.authsvc.fetch(c.req.clone())
        let user: UserJwt = results.json();
        return c.text("User: " +;

Results is User: undefined

You probably need to await results.json(). In cases like this, I’d recommend adding some debug logging like console.log(user) so you can during development quickly spot issues.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.