0

I have a service call that accepts the following model:

public class ServiceModel {
   public DataModel dModel {get; set;}
   public JObject schema {get; set;}
}

The DataModel is responsible for holding data that will be used to populate user defined schema (see below).

The schema is a user defined (at runtime) dynamic json structure that contains tokenized values like this. Being that it's user defined, it can be deeply nested.

{
   "id": "<tokenized_id>",
   "hero":{
       "heroName": "<tokenized_heroName>", 
       "heroType": "<tokenized_heroType>",
       "heroSkill": "<tokenized_heroSkill>",
       "heroArmor": {
          "armor_id": "<tokenized_armorId>",
          ...
       }
    }
}

What I want to do is pull data from the DataModel and replace the corresponding tokenized value with it. The tricky part comes from the possibility of deeply nested objects

My first idea is to just flatten the schema into a string and doing a Find/Replace on the entire string but I'm curious if there's a more elegant way.

There's the option of working with JObjects or even Dictionary but neither provide a nice way to access nested objects. I believe I would need to use recursion which could get ugly.

Are there betters ways to accomplish this?

3
  • What about using SelectToken? See Replace part of a JSON with other (using a string token). Commented May 14, 2019 at 5:37
  • @dbc I don't believe SelectToken can help me here as I will not know the path to each node. I appreciate your time! Commented May 14, 2019 at 20:57
  • Then how do you know what to replace? I think a concrete minimal reproducible example would increase your chances of getting help. Commented May 14, 2019 at 21:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.