ZZZSharePoint
ZZZSharePoint

Reputation: 1351

Add a list in an existing JSON value

So I have below call to a method where my argument is a json string of this type

var jsonWithSearchData = await querySearchData(jsonOut);

jsonOut ->

[
    {
        "data": {
            "_hash": null,
            "kind": "ENY",
            "id": "t123",
            "payload": {
                "r:attributes": {
                    "lok:934": "@0|I"
                    
                },
                "r:relations": {
                    "lok:1445": "15318",
                    "lok:8538": "08562"
                    
                },
                "r:searchData": "",
                "r:type": [
                    "5085"
                ]
            },
            "type": "EQT",
            "version": "d06"
        }
    }
]

The querySearchData() returns me two list something like this :["P123","P124","P987"] and ["Ba123","KO817","Daaa112"]

I want to add this list in my r:searchData key above. The key inside my searchData i.e. r:Porelation and ClassA and ClassB remains static. So I would like my searchData in my input Json to finally become something like this.

"r:searchData": { 
    "r:Porelation":{
       "ClassA": ["P123","P124","P987"],
       "ClassB": ["Ba123","KO817","Daaa112"]
    }
},

How can I do this? What I tried:

JArray jfinObject = JArray.Parse(jobjects);
jfinObject["r:searchData"]["r:relations"]["ClassA"] = JArray.Parse(ListofCode.ToString());
       

And I get below error:

System.Private.CoreLib: Exception while executing function: Function1. Newtonsoft.Json: Accessed JArray values with invalid key value: "r:searchData". Int32 array index expected.

Upvotes: 0

Views: 2326

Answers (1)

haldo
haldo

Reputation: 16701

There are a few ways you can add a node/object/array to existing json.

One option is to use Linq-to-Json to build up the correct model. Assuming you have the json string described in your question, the below code will add your desired json to the r:searchData node:

var arr = JArray.Parse(json);  // the json string
var payloadNode = arr[0]["data"]["payload"];

// use linq-to-json to create the correct object
var objectToAdd = new JObject(
                      new JProperty("r:Porelation", 
                        new JObject(
                            new JProperty("r:ClassA", array1), 
                            new JProperty("r:ClassB", array2))));

payloadNode["r:searchData"] = objectToAdd;

where array1 and array2 above could come from a linq query (or just standard arrays).

// Output: 
{
  "data": {
    "_hash": null,
    "kind": "ENY",
    "id": "t123",
    "payload": {
      "r:attributes": {
        "lok:934": "@0|I"
      },
      "r:relations": {
        "lok:1445": "15318",
        "lok:8538": "08562"
      },
      "r:searchData": {
        "r:Porelation": {
          "r:ClassA": [
            "P123",
            "P456"
          ],
          "r:ClassB": [
            "Ba123",
            "Ba456"
          ]
        }
      },
      "r:type": [
        "5085"
      ]
    },
    "type": "EQT",
    "version": "d06"
  }
}

Online demo


Another option is to create the json from an object, which could be achieved using JToken.FromObject(). However, this will only work if you have property names which are also valid for C# properties. So, this won't work for your desired property names as they contain invalid characters for C# properties, but it might help someone else:

// create JToken with required data using anonymous type
var porelation = JToken.FromObject(new 
{ 
    ClassA = new[] { "P123", "P456" },   // replace with your arrays here
    ClassB = new[] { "Ba123", "Ba456" }  // and here
});

// create JObject and add to original array
var newObjectToAdd = new JObject(new JProperty("r:Porelation", porelation));
payloadNode["r:searchData"] = newObjectToAdd;

Upvotes: 2

Related Questions