<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/model-variable-json-field-types-with-unions-4ugh6mlue" -->

---
title: Model variable JSON field types with unions | daily.dev
description: Revisits handling a JSON field whose type varies between string and number in .NET, this time using the new union types feature coming in C# 15 instead of a...
canonical: https://daily.dev/posts/model-variable-json-field-types-with-unions-4ugh6mlue
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Model variable JSON field types with unions | daily.dev
og:description: Revisits handling a JSON field whose type varies between string and number in .NET, this time using the new union types feature coming in C# 15 instead of a...
og:url: https://daily.dev/posts/model-variable-json-field-types-with-unions-4ugh6mlue
og:image: https://api.daily.dev/og/posts/4UGh6Mlue.png
og:image:alt: Model variable JSON field types with unions
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Model variable JSON field types with unions

**[Damir's Corner](https://daily.dev/sources/damirscorner)** · 3 min read · 6 upvotes · 0 comments

## Summary

Revisits handling a JSON field whose type varies between string and number in .NET, this time using the new union types feature coming in C# 15 instead of a custom serializer. A StringOrNumber union type is defined with a custom StringValue property to normalize access, and JsonSerializer.Deserialize correctly types the field for both string and numeric JSON representations, including null handling. A sample project with tests is provided on GitHub. Trying this requires the latest .NET 11 SDK pre-release (Release Candidate 1), which ships with a go-live license, though the author recommends waiting for the final November release.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.damirscorner.com/blog/posts/20260925-ModelVariableJsonFieldTypesWithUnions.html>

## Questions this post answers

### How can I deserialize a JSON field that is sometimes a string and sometimes a number in C#?

Define a union type combining string and long, such as 'public union StringOrNumber(string, long)', and use it as the property type in the model class. JsonSerializer.Deserialize then correctly types the value whether the JSON field is a string like "1.0.0" or a number like 1, without needing a custom serializer, and a custom property can expose a normalized string form.

_See the full working example on daily.dev for handling variable-type JSON fields with C# unions._

### What .NET SDK version do I need to use union types in C# 15?

Union types require the latest .NET 11 SDK pre-release, specifically Release Candidate 1 at the time of writing. It ships with a go-live support license, meaning it can technically be deployed to production, though waiting for the final release in November is recommended for stability.

_Track .NET 11 release readiness on daily.dev before adopting C# 15 union types in production._

## Similar posts on daily.dev

- [The pain of serializing unions and closed class hierarchies with System.Text.Json](https://daily.dev/posts/the-pain-of-serializing-unions-and-closed-class-hierarchies-with-system-text-json-5gdr03rjh) · .NET Escapades · 14 upvotes · 1 comments
- [Use C\# unions and closed hierarchies in ASP.NET Core](https://daily.dev/posts/use-c-unions-and-closed-hierarchies-in-asp-net-core-2iyzzepqi) · ASP.NET Blog · 44 upvotes · 2 comments

---

Tags: [#.net](https://daily.dev/tags/.net), [#c#](https://daily.dev/tags/c#)

[View this post on daily.dev](https://daily.dev/posts/model-variable-json-field-types-with-unions-4ugh6mlue)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Model variable JSON field types with unions","url":"https://daily.dev/posts/model-variable-json-field-types-with-unions-4ugh6mlue","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/model-variable-json-field-types-with-unions-4ugh6mlue"},"datePublished":"2026-09-25T06:28:14.373Z","dateModified":"2026-09-25T06:28:33.981Z","description":"Revisits handling a JSON field whose type varies between string and number in .NET, this time using the new union types feature coming in C# 15 instead of a...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/425073b57495acf9675afac1769c56ee?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/425073b57495acf9675afac1769c56ee?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Damir's Corner","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Damir's Corner","logo":"https://media.daily.dev/image/upload/s--9Wkl5ofQ--/f_auto,q_auto/v1780213496/logos/damirscorner?_a=BAMAMiWQ0","url":"https://daily.dev/sources/damirscorner"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/model-variable-json-field-types-with-unions-4ugh6mlue","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":6},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":".net,c#","timeRequired":"PT3M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Damir's Corner","item":"https://daily.dev/sources/damirscorner"},{"@type":"ListItem","position":3,"name":"Model variable JSON field types with unions"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/model-variable-json-field-types-with-unions-4ugh6mlue#faq","mainEntity":[{"@type":"Question","name":"How can I deserialize a JSON field that is sometimes a string and sometimes a number in C#?","acceptedAnswer":{"@type":"Answer","text":"Define a union type combining string and long, such as 'public union StringOrNumber(string, long)', and use it as the property type in the model class. JsonSerializer.Deserialize then correctly types the value whether the JSON field is a string like \"1.0.0\" or a number like 1, without needing a custom serializer, and a custom property can expose a normalized string form. See the full working example on daily.dev for handling variable-type JSON fields with C# unions."}},{"@type":"Question","name":"What .NET SDK version do I need to use union types in C# 15?","acceptedAnswer":{"@type":"Answer","text":"Union types require the latest .NET 11 SDK pre-release, specifically Release Candidate 1 at the time of writing. It ships with a go-live support license, meaning it can technically be deployed to production, though waiting for the final release in November is recommended for stability. Track .NET 11 release readiness on daily.dev before adopting C# 15 union types in production."}}]}
```

