1

I am trying to write simple Json Array.
I m bit rusty with this Json as I have just started learning it with Vb.Net and Using NewtonSoft.Json Library

Sorry if there is something wrong in question. As I said that I just started Json so please advice to rectify.

Old Json:

[{
    "YEAR": "2018-2019",
    "COMPNO": "1",
    "TYPE": "SAL",
    "Field1": false  
  },
  {
    "YEAR": "2018-2019",
    "COMPNO": "2",
    "TYPE": "PUR",
    "Field1": false
  }]

I want a field to add in the object like

New Json:

[{
    "YEAR": "2018-2019",
    "COMPNO": "1",
    "TYPE": "SAL",
    "Field1": false,
    "Field2": false   '-----------------------Something Like this
  },
  {
    "YEAR": "2018-2019",
    "COMPNO": "2",
    "TYPE": "PUR",
    "Field1": false,
    "Field2": false     '--------------In every object if possible.
  }]

My Json is simple without groups.
I know how to add a new Object inside the Jarray but I want to add a field
How can I achieve this task?
P.S I am using Newtonsoft.JSON libraries.

3
  • 1
    If you add a comma between "Field1": false and "Field2": false, that's a vaid JSON. Are you asking how to modify the JSON or the classes that store/handle it? Commented Dec 21, 2018 at 12:09
  • 1
    We would need to see how you are currently doing it. Commented Dec 21, 2018 at 12:49
  • @Jimi My mistake , I forgot a ',' but I hope you understood what I wanted.. Commented Dec 22, 2018 at 6:17

1 Answer 1

2

Edit switched to VB

   Dim initialJson = "[{" & vbCrLf & "    ""YEAR"": ""2018-2019""," & vbCrLf & "    ""COMPNO"": ""1""," & vbCrLf & "    ""TYPE"": ""SAL""," & vbCrLf & "    ""Field1"": false  " & vbCrLf & "  }," & vbCrLf & "  {" & vbCrLf & "    ""YEAR"": ""2018-2019""," & vbCrLf & "    ""COMPNO"": ""2""," & vbCrLf & "    ""TYPE"": ""PUR""," & vbCrLf & "    ""Field1"": false" & vbCrLf & "  }]"

    Dim array = JArray.Parse(initialJson)
    For Each item In array
        item("Field2") = False
    Next

    Dim Result = array

If you are creating that json I would suggest doing it before you create it but if you just need to update that array.

        var initialJson = "[{\r\n    \"YEAR\": \"2018-2019\",\r\n    \"COMPNO\": \"1\",\r\n    \"TYPE\": \"SAL\",\r\n    \"Field1\": false  \r\n  },\r\n  {\r\n    \"YEAR\": \"2018-2019\",\r\n    \"COMPNO\": \"2\",\r\n    \"TYPE\": \"PUR\",\r\n    \"Field1\": false\r\n  }]";

        var array = JArray.Parse(initialJson);

        array.ToList().ForEach(item => item["Field2"] = false);

        var result = array;
Sign up to request clarification or add additional context in comments.

6 Comments

Good catch will change that real quick.
Don't forget to the ToString() to convert the JArray back to JSON
@RyanSchlueter The answer I got from you is real quick. Thanx Again. Just one thing I want to ask. Can you tell me code to check if Field2 exists in each item. Something like if not Field2 exists then item("Field2") = False end if
I believe there is a contains key method or you can check if item("Field2") = =null before setting it. If that doesn't work I can look when get to computer
Your assumptions are also right :). I don't know about 'contains' keyword but I coded something like this in Vb.net --> if item(Field2) = nothing then item(Field2) = False and it worked... Thanx a lot. @RyanSchlueter
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.