© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
1 reply
Jure

How to use JSON column type?

Hi. I'm new to Drizzle and 'm strugling with JSON column type.

Here is the example:

interface Translation {
  __: string
  hr?: string
  cs?: string
  de?: string
  en?: string
}

// table schema
export const nodes = mysqlTable('nodes', {
  // ...
  name: json('value').$type<Translation>().notNull(),
  // ...
}

// inserting JSON
await db.insert(models.nodes)
  .values({
    //...
    // node.name = {
    //   "__": "Moda",
    //   "hr": "Moda",
    //   "cs": "Móda",
    //   "en": "Fashion",
    //   "de": "Mode"
    // }
    name: node.name
    //...
  })
interface Translation {
  __: string
  hr?: string
  cs?: string
  de?: string
  en?: string
}

// table schema
export const nodes = mysqlTable('nodes', {
  // ...
  name: json('value').$type<Translation>().notNull(),
  // ...
}

// inserting JSON
await db.insert(models.nodes)
  .values({
    //...
    // node.name = {
    //   "__": "Moda",
    //   "hr": "Moda",
    //   "cs": "Móda",
    //   "en": "Fashion",
    //   "de": "Mode"
    // }
    name: node.name
    //...
  })


So far everything looks good. But I'm confused when selecting the JSON column name;

// selecting JSON
const nodes = await db.select().from(models.nodes)

// this is valid typescript code but is always undefined.
console.log(nodes[0].name.en)

// this is not valid typescript code, but works.
console.log(JSON.parse(nodes[0].name).en)
// selecting JSON
const nodes = await db.select().from(models.nodes)

// this is valid typescript code but is always undefined.
console.log(nodes[0].name.en)

// this is not valid typescript code, but works.
console.log(JSON.parse(nodes[0].name).en)


So the problem is that I get nice typescript support when selecting JSON column, because type is passed to
json().$type<>
json().$type<>
, but selected "name" is just a string which need to be parsed.

What am I missing?
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

jsdoc for json type column
Drizzle TeamDTDrizzle Team / help
2y ago
How to filter json column
Drizzle TeamDTDrizzle Team / help
2y ago
Json column type returning as plain string?
Drizzle TeamDTDrizzle Team / help
16mo ago
How to seed custom json type
Drizzle TeamDTDrizzle Team / help
3mo ago