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?

dbc
  • 104,963
  • 20
  • 228
  • 340
Martelyn
  • 83
  • 1
  • 5
  • What about using [`SelectToken`](https://www.newtonsoft.com/json/help/html/SelectToken.htm)? See [Replace part of a JSON with other (using a string token)](https://stackoverflow.com/a/33056210). – dbc May 14 '19 at 05: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! – Martelyn May 14 '19 at 20:57
  • Then how do you know what to replace? I think a concrete [mcve] would increase your chances of getting help. – dbc May 14 '19 at 21:01

0 Answers0