Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add Json in Var using an array

I am trying to add below JSON in VAR as an array.

[
{
    "path": "test",
    "op" : "replace",
    "from" : "null",
    "value" :"7c45f39c-708b-4d54-8fff-421640dyuuii"
}

]

I did try, below and it didn’t work me, how can declare this as an array, TIA.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var data = new
        {
         path="test",
         op ="replace",
         from = "null",
         value ="7c45f39c-708b-4d54-8fff-421640dyuuii"
        };

>Solution :

The syntax new { ... } declares an object. You can use new[] { } to declare an array. And then within that declaration you can declare you object(s). For example:

var data = new[] {
    new {
       path = "test",
       op = "replace",
       from = "null",
       value = "7c45f39c-708b-4d54-8fff-421640dyuuii"
    }
};

As an aside… Note that with implicit types you can’t declare an empty array, such as:

var data = new[] { };

As the compiler would need an example of the object to infer the type. In the example above the compiler infers an anonymous type from the inner new { ... } declaration of your object. Any type would work, there just needs to be one. Otherwise you’d need to explicitly define a type. For example:

var data = new string[] {};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading