1

i have a json data as below, i wanted to add new entry to "revs" array if Item_id match, let say Item_id i was looking at is 1 and the existing json data consist of two item inside "revs" array, and wanted to add new entry

{
   "Root" : [
        {
             "Item_Name" : "Test",
             "Items" : [
                {
                    "Item_id" : "1",
                    "revs" : [
                        {
                            "rev_id" : "1"
                        },
                        {
                            "rev_id" : "3"
                        },
                        {
                            "rev_id" : "need to add new entry here"
                        }
                    ]
                },
                {
                    "Item_id" : "2",
                    "revs" : [
                        {
                            "rev_id" : "1"
                        }
                    ]   
                }
            ]
        }
    ]
}

i parse the json data like this

JObject jsonObject = JObject.Parse(<the json data above>);

iterate into "revs" array after Item_id matched and i create a JObject assiged to new entry data

JObject new_rev = new JObject();
new_rev["rev_id"] = "need to add new entry here"

what should i do to make my new_rev data reflect in jsonObject?

p/s: i use jsoncpp for C++ before and i just loop through with reference object and i can have modified json data into it easily

thanks.

2 Answers 2

2

Assuming Root only have one item in the array and assuming Item_id will be unique

    JObject jsonObject = JObject.Parse(<the json string>);
    JObject new_rev = new JObject();
    new_rev["rev_id"] = "need to add new entry here";
    JArray items = jsonObject["Root"][0]["Items"].Value<JArray>();
    foreach(var item in items){
        if(item.Value<JObject>()["Item_id"].Value<string>() == "1"){
            item["revs"].Value<JArray>().Add(new_rev);
            break;
        }
    }

See it here https://dotnetfiddle.net/IYVULd

Sign up to request clarification or add additional context in comments.

1 Comment

your answer seems working, but the json data i gave is just example, actually there is multiple roots and multiple items, anyway thanks for your help.
1

I try to find out better way to add in the data, in the end realise LINQ (method syntax) make my life easier, the following code is working for me, eventhough contains multiple Root, Items

Sorry, i am still new in LINQ, perhaps someone can enlighten me with query syntax that do the same

JObject jsonObject = JObject.Parse(<existing json data>);
JObject newNode = new JObject();
newNode["rev_id"] = "need to add new entry here"

jsonObject["Root"].Children()
                       .Where(w => w["Item_Name"].ToString() == selectedItemName)
                       .Select(s => s["Items"]).Children()
                       .Where(w => w["Item_id"].ToString() == selectedItemId)
                       .Select(s => s["revs"]).Children()
                       .LastOrDefault().AddAfterSelf(newNode);

Comments

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.