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

  • SEARCH
  • Home
  • 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 6811383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:19:14+00:00 2026-05-26T20:19:14+00:00

What I have I have templates that are stored in a database, and JSON

  • 0

What I have
I have templates that are stored in a database, and JSON data that gets converted into a dictionary in C#.

Example: 

Template: “Hi {FirstName}”

Data: “{FirstName: ‘Jack’}”

This works easily with one level of data by using a regular expression to pull out anything within {} in the template.

What I want
I would like to be able to go deeper in the JSON than the first layer.

Example:

Template: “Hi {Name: {First}}”

Data: “{Name: {First: ‘Jack’, Last: ‘Smith’}}”

What approach should I be taking? (and some guidance on where to start with your pick)

  1. A regular expression
  2. Not use JSON in the template (in favor of xslt or something similar)
  3. Something else

I’d also like to be able to loop through data in the template, but I have no idea at all where to start with that one!

Thanks heaps

  • 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-05-26T20:19:14+00:00Added an answer on May 26, 2026 at 8:19 pm

    Here is how I would do it:

    Change your template to this format Hi {Name.First}

    Now create a JavaScriptSerializer to convert JSON in Dictionary<string, object>

    JavaScriptSerializer jss = new JavaScriptSerializer();
    dynamic d = jss.Deserialize(data, typeof(object));
    

    Now the variable d has the values of your JSON in a dictionary.

    Having that you can run your template against a regex to replace {X.Y.Z.N} with the keys of the dictionary, recursively.

    Full Example:

    public void Test()
    {
        // Your template is simpler
        string template = "Hi {Name.First}";
    
        // some JSON
        string data = @"{""Name"":{""First"":""Jack"",""Last"":""Smith""}}";
    
        JavaScriptSerializer jss = new JavaScriptSerializer();
    
        // now `d` contains all the values you need, in a dictionary
        dynamic d = jss.Deserialize(data, typeof(object));
    
        // running your template against a regex to
        // extract the tokens that need to be replaced
        var result = Regex.Replace(template, @"{?{([^}]+)}?}", (m) =>
            {
                // Skip escape values (ex: {{escaped value}} )
                if (m.Value.StartsWith("{{"))
                    return m.Value;
    
                // split the token by `.` to run against the dictionary
                var pieces = m.Groups[1].Value.Split('.');
                dynamic value = d;
    
                // go after all the pieces, recursively going inside
                // ex: "Name.First"
    
                // Step 1 (value = value["Name"])
                //    value = new Dictionary<string, object>
                //    {
                //        { "First": "Jack" }, { "Last": "Smith" }
                //    };
    
                // Step 2 (value = value["First"])
                //    value = "Jack"
    
                foreach (var piece in pieces)
                {
                    value = value[piece]; // go inside each time
                }
    
                return value;
            });
    }
    

    I didn’t handle exceptions (e.g. the value couldn’t be found), you can handle this case and return the matched value if it wasn’t found. m.Value for the raw value or m.Groups[1].Value for the string between {}.

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

Sidebar

Related Questions

I have a index.php file that will include several external files: content/templates/id1/template.php content/templates/id2/template.php content/templates/id3/template.php
Good day, i have mongodb database filled with some data, i ensured that data
I have several PDF templates that I would like to load and modify and
I have a bunch of templates that are used for rpc and was wondering
I have a class that gathers templates and displays the final output after connecting
I am rather new to django templates and have an impression that I have
I have heard from many people that usage of templates make the code slow.
I have a Rails 2.1.2 site that only has html templates e.g. jobs.html.erb, so
In my Django templates, I have a couple pieces of code that are like
I have template that looks like this: 100 template<size_t A0, size_t A1, size_t A2,

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.