1

I have dynamic nested JSON coming from front-end. See the below, as dynamic as only those keys whose value ain't null are sent. In these case, middle initial isn't been filled, so you don't find it:

{
    "name": {
        "firstname": "fst",
        "lastname": "lst"
    },
    "gender": "male",
    "birthdate": "2021-02-09",
    "maritalstatus": "Never married",
    "id": {
        "social": "123456789"
    }
}

I've tried these for each potential properties:

JsonElement lastname;
question1.TryGetProperty("lastname", out lastname);

But looking for a more decent way, for example the below one:

var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true,
                IgnoreNullValues = true
            };

var jsonModel = JsonSerializer.Deserialize<Survey>(obj, options);
var modelJson = JsonSerializer.Serialize(jsonModel, options);

But the problem is this way only can deal with the first-level properties, like gender, birthdate, maritalstatus, but invalid with firstname, lastname and social.

How to solve it or any other approaches I can try, many thanks!

UPDATE1:

Survey is a poco looks like this but much more than this properties, see those annotations, I'm trying to let itself do the mapping instead of on my own:

public class Survey
    {
        [JsonPropertyName("firstname")]
        public string FirstName{get;set;}

        [JsonPropertyName("middleinitial")]
        public char MiddleInitial{get;set;}

        [JsonPropertyName("lastname")]
        public string LastName{get;set;}

        [JsonPropertyName("jrsr")]
        public string JrSr{get;set;}


        [JsonPropertyName("gender")]
        public char Gender{get;set;}

        [JsonPropertyName("birthdate")]
        public DateTime Birthdate{get;set;}


        [JsonPropertyName("maritalstatus")]
        public string MaritalStatus{get;set;}


        [JsonPropertyName("Social")]
        public string SocialSecurityNumber{get;set;}

        public string MedicareNumber{get;set;}
        public string MedicaidNumber{get;set;}


    }
Alex
  • 601
  • 8
  • 22
  • What's the declaration of `Survey` look like? – Connor Low Feb 26 '21 at 17:37
  • 1
    [Does this answer your question](https://stackoverflow.com/a/33094930/6789816)? – Connor Low Feb 26 '21 at 18:15
  • 1
    @ConnorLow Thank you, that solution works, so actually a customized json converter and plus reflection, which take the annotation, "name.firstname", inside the converter, it has SelectToken method takes care of it. – Alex Feb 26 '21 at 20:06

1 Answers1

1

Try this :

 dynamic model = JsonConvert.DeserializeObject(jsonStr);

And access what ever you want :

var firstName = model.name.firstname;

UPDATE 1 : You should define different models for complex fields.

    public class Survey
    {

        public NameModel Name { get; set; }

        public IdModel Id { get; set; }

        public char MiddleInitial { get; set; }
        public string JrSr { get; set; }
        public string Gender { get; set; }
        public DateTime Birthdate { get; set; }
        public string MaritalStatus { get; set; }
        public string MedicareNumber { get; set; }
        public string MedicaidNumber { get; set; }
    }

    public class NameModel
    {
        public string LastName { get; set; }

        public string FirstName { get; set; }
    }

    public class IdModel
    {
        public string Social { get; set; }
    }

Additionally there is no need to use JsonPropertyName while field names in model and JSON are equal.

  • Thanks, this one better than the try method, but still need to iterate each property on my own. Can we somehow let itself do the mapping? – Alex Feb 26 '21 at 17:49
  • @Alex when it comes to dealing with JSON, we either know its properties and define a model to deserialize, or the JSON changes dynamically. As you updated your answer, you should define different models for complex properties like ***name*** and ***id***, I updated my answer. – Mehdi Fathipour Feb 26 '21 at 18:22