VariableJson 1.1.0

dotnet add package VariableJson --version 1.1.0
                    
NuGet\Install-Package VariableJson -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="VariableJson" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="VariableJson" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="VariableJson" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add VariableJson --version 1.1.0
                    
#r "nuget: VariableJson, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package VariableJson@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=VariableJson&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=VariableJson&version=1.1.0
                    
Install as a Cake Tool

VariableJson (vjson)

GitHub .NET .NET 6 Nuget Nuget Nuget (with prereleases)

vjson is a JSON parser that adds support for variables.

Supported languages

Examples

The simplest example is

{
  "$vars": {
    "name": "John Doe"
  },
  "johndoe": "$(name)"
}

which gets converted to

{
  "johndoe": "John Doe"
}

Variables can also reference other variables

{
  "$vars": {
    "name": "John Doe",
    "greeting": "Hello $(name)"
  },
  "johndoe": "$(greeting)"
}

which generates

{
  "johndoe": "Hello John Doe"
}

You can nest objects and arrays with each other and all other non-complex data types. Here is a complex sample

{
  "$vars": {
    "name": "John Doe",
    "greeting": "Hello $(name)",
    "age": 42,
    "address": {
      "street": "123 Main St",
      "city": "Anytown",
      "state": "CA",
      "zip": "12345"
    },
    "phone": ["000-123-4567", "000-123-4568"]
  },
  "johndoe": {
    "name": "$(name)",
    "greeting": "$(greeting)",
    "age": "$(age)",
    "address": "$(address)",
    "phone": "$(phone)"
  }
}

which would give you

{
  "johndoe": {
    "name": "John Doe",
    "greeting": "Hello John Doe",
    "age": 42,
    "address": {
      "street": "123 Main St",
      "city": "Anytown",
      "state": "CA",
      "zip": "12345"
    },
    "phone": ["000-123-4567", "000-123-4568"]
  }
}

You can reference objects and values that are in an array by specifying the index

{
  "$vars": {
    "array": [1, false, true, "hello, world!", null]
  },
  "first": "$(array.0)"
}

which would produce

{
  "first": 1
}

You cannot reference objects that are not stored in the variable container. For example, this will not work

{
  "$vars": {
    "hello": "world!"
  },
  "fizz": "$(buzz)",
  "buzz": "$(hello)"
}

This will throw an exception because fizz references a variable that does not exist in the variable container.

Lastly, you can also reference environment variables using $[EnvironmentVariableName]

{
  "PATH": "$[PATH]"
}

When referencing only environment variables you do not need to specify a variable container, it won't be used. You may use both environment variables and variables in the same JSON file.

{
  "$vars": {
    "name": "John Doe"
  },
  "johndoe": "$(name)",
  "PATH": "$[PATH]"
}

which produces

{
  "johndoe": "John Doe",
  "PATH": "..." // varies by system
}

Usage

Note This library uses the System.Text.Json library for JSON parsing and serialization and does not handle any exceptions that may be thrown by that library. You should handle any thrown exception yourself.

VariableJson only parses and produces JSON, it does not provide a mechanism for deserializing the JSON into an object. You can use System.Text.Json, Newtonsoft.Json, or any other JSON library to deserialize the JSON into an object.

string originalJson = File.ReadAllText("path/to/file.json");
string convertedJson = VariableJson.Json.Parse(originalJson);

VariableJsonOptions

You can specify some options when parsing JSON using the VariableJsonOptions class.

string originalJson = File.ReadAllText("path/to/file.json");
string convertedJson = VariableJson.Json.Parse(originalJson, new VariableJsonOptions { VariableKey = "myVars" });

The following options are available:

VariableKey - The name of the variable container. Defaults to $vars.

Delimiter - The delimiter to use when parsing variables. Defaults to . (period). This string should not appear in any of your JSON key names.

MaxRecurse - The maximum number of times to recurse when resolving variables. Defaults to 1024.

KeepVars - Whether or not to keep the variable container in the output. Defaults to false. The variable container will be identical to the one in the input. It's value will not be resolved.

EmittedName - The name of the variable container in the output. Defaults to $vars. Only used if KeepVars is true.

JSON Schema

While vjson itself is valid JSON, it uses special markers to denote variables. This means that you can't use these same markers in string-type values. To identify that you want to use the variable value instead, you should wrap the variable name in $(variableName) and set it as a string value.

{
  "$vars": {
    "name": "John Doe"
  },
  "johndoe": "$(name)"
}

If you don't want to use $vars as the variable container, you can use the VariableJsonOptions class to specify a different variable container name.

string originalJson = File.ReadAllText("path/to/file.json");
string convertedJson = VariableJson.Json.Parse(originalJson, new VariableJsonOptions { VariableKey = "myVars" });

In the above example, this will cause variable lookups to be performed in the myVars object instead of the $vars object.

{
  "myVars": {
    "name": "John Doe"
  },
  "johndoe": "$(name)"
}

Performance

vjson deserializes the JSON document and then resolves variable references recursively. Once the document has been parsed, it then serializes the resultant object back to JSON to generated the final output. You can then use this output as you would with any JSON parsing library, such as System.Text.Json or Newtonsoft.Json, for example.

There's currently no variable lookup caching, but this is a planned feature that you'll be able to opt into using the VariableJsonOptions class.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 432 1/21/2023
1.1.0-beta 246 1/21/2023
1.0.1 393 1/16/2023
1.0.0 650 1/16/2023 1.0.0 is deprecated.
0.1.0-beta 433 1/16/2023 0.1.0-beta is deprecated.