Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8276475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:23:08+00:00 2026-06-08T08:23:08+00:00

So, I have some JSON I’m deserialising. Bear in mind I don’t have control

  • 0

So, I have some JSON I’m deserialising. Bear in mind I don’t have control of the data I’m getting; I just have to work with it as best I can.

It was going fairly well, until I ran into an issue where in an item ("z" below) can be two different types (see "z":[] and "z":{}) within the same item type (in the data I’m dealing with, things should be considered a Dictionary<string, FooType> after deserialisation…).

{
"things":{
    "1":{
        "x":123.45,
        "y":678.90,
        "z":{
                "2":true
            },
        "a":[
                1,
                2
            ]
        },
    "2":{
        "x":1414.23,
        "y":5656.78,
        "z":[
                true
            ],
        "a":[
                3
            ]
        }
    }
}

I’m pretty sure this is an output bug (i.e. the source is producing "z":[true] when it should produce "z":{"0":true}) because this is how "z":[true] is to be interpreted later in the application. However, as I said, I don’t have control of the data, and the issue is unlikely to be fixed any time soon.

I’ve tried having a member variable for each, but the naming conflict (from using [DataContract(Name = “z”)]) causes an exception (unsurprisingly).

How can I read both types of "z" into a common FooType? (Or equivalent, such as two subclasses of a common FooParentType that somehow get applied only in the correct cases…)

EDIT: I’d prefer to achieve the desired result as part of the deserialisation process, without preprcossing the string (although if there are no other ways, I’ll go there, I guess…)

I’m using C#, Mono, JSON.NET, and using code like so (compilable version on pastebin):

string jsonString = "{\"things\":{\"1\":{\"x\":123.45,\"y\":678.9,\"z\":{\"2\":true},\"a\":[1,2]},\"2\":{\"x\":1414.23,\"y\":5656.78,\"z\":[true],\"a\":[3]}}}";
RootType rt = JsonConvert.DeserializeObject<RootType>(jsonString);

With classes:

[DataContract]
class RootType
{
    [DataMember(IsRequired = true)]
    public Dictionary<string, FooType> things { get; set; }
}

[DataContract]
class FooType
{
    [DataMember(IsRequired = true)]
    public double x { get; set; }

    [DataMember(IsRequired = true)]
    public double y { get; set; }

    [DataMember(IsRequired = true)]
    public List<int> a { get; set; }

    // List<bool> zList and Dictionary<string, bool> zDict each only work for one case
    // but not the other, and having both causes a naming conflict
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:23:09+00:00Added an answer on June 8, 2026 at 8:23 am

    I had a similar issue and simply ran the string through a string.replace function to “correct” the data prior to deserializing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data() object containing some json. Is there a way I can
I have some JSON data coming from a service. I don't know the exact
I have some data and need to create a json file with this structure
I have some data that I have to serialize to JSON. I'm using JSON.NET.
I have a REST api which dumps some json data (user info, etc). Now
I have some JSON data that looks like this: { 910719: { id: 910719,
I have some code that requests some JSON from an API. When data is
I have some json in this structure: {data:{1:file1.gif,2:file2.jpg}} // ... etc I am trying
I have two DND conatainers, SourceContainer and TargetContainer SourceContainer have some json data e.g:
I have some difficulties with json deserialization using GSon and I hope somebody can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.