0

I'm working in UIPath and trying to put together a single JSON configuration file for a library. My hands are sort of tied as far what functionality in VB.Net I can use but I'd like to filter my properties by environment. I'm able to successfully filter out the environment I'm not concerned with but I'm stuck with the existing environment property at the highest level of the JSON structure. I want to get rid of that one too after I know which one I want to keep.

Starting Point

{
"Development": {
    "Database": {
        "DB 1": {
            "Server": "Foo",
            "Application Name": "App1"
        },
        "DB 2": {
            "Server": "Boo",
            "Application Name": "App2"
        }
    }
},
"Production": {
    "Database": {
        "DB 1": {
            "Server": "FooFoo",
            "Application Name": "App1"
        },
        "DB 2": {
            "Server": "BarBar",
            "Application Name": "App2"
        }
    }
}

What I want to get to based on Environment (Development or Production)

{
"Database": {
    "DB 1": {
        "Server": "Foo",
        "Application Name": "App1"
    },
    "DB 2": {
        "Server": "Boo",
        "Application Name": "App2"
    }
}

Here is what I've tried so far:

  1. Read JSON File to String

  2. Deserialize to Newtonsoft JObject

  3. Filter by environment

     configData.Descendants
           .OfType(Of JProperty)
           .Where(Function(attr) attr.Name.ToUpper = "PRODUCTION" Or attr.Name.ToUpper = "DEVELOPMENT" And attr.Name.ToUpper <> environment.ToUpper)
           .ToList()
           .ForEach(Sub(attr) attr.Remove())
    

Result

{
"Development": {
    "Database": {
        "DB 1": {
            "Server": "Foo",
            "Application Name": "App1"
        },
        "DB 2": {
            "Server": "Boo",
            "Application Name": "App2"
        }
    }
}

From here is where I'm stuck and can't seem to get anything to work. I want to be able to access my configuration properties without the caller knowing what environment its in.

Something like

configuration("Database")("DB 1")

Versus

configuration(environment)("Database")("DB 1")

If I'm taking a stupid approach to this altogether I'd also accept that as an answer as well. I've never really played with VB or .Net before so its all fairly new to me. I'm understanding some things but am far from really understanding anything powerful.

  • Does `JsonExtensions.RemoveAllExcept(this TJToken obj, IEnumerable paths)` from [this answer](https://stackoverflow.com/a/30333562/3744182) to [How to perform partial object serialization providing “paths” using Newtonsoft JSON.NET](https://stackoverflow.com/q/30304128/3744182) meet your needs? – dbc Nov 07 '20 at 00:30

1 Answers1

0
Dim environment = "Development"
Dim filteredJson = JObject.Parse(yourJson)(environment).ToString

will give you

{
  "Database": {
    "DB 1": {
      "Server": "Foo",
      "Application Name": "App1"
    },
    "DB 2": {
      "Server": "Boo",
      "Application Name": "App2"
    }
  }
}

Substitute "Production" for "Development" when appropriate, of course.

Note that you have a missing closing brace in your json samples

stevec
  • 62
  • 1
  • 1
  • 5