1
{
    "version": "15.1",
    "count": 8,
    "data": [
        {
            "furit_items": [
                {
                    "price": 12.6,
                    "base_price": 6.59,
                    "text": "banana",
                    "product_id": "1234",
                    "product_name": "banana",
                    "quantity": 1
                }
            ],
            "product_sub_total": 12.6,
            "product_total": 12.6,
            "shipments": []
        }
    ]
}

This is my json data getting through a service.

Is this possible to add a new property {"quality":"good"} under "furit_items" object?

2
  • What is class of this object? Commented Jun 25, 2015 at 6:30
  • Don't have class . I am getting this json through service. Commented Jun 25, 2015 at 6:34

4 Answers 4

3

Try this:

dynamic original = JsonConvert.DeserializeObject(json, typeof(object));
original.data[0].furit_items[0].quality = good;
var modifiedJson = JsonConvert.SerializeObject(original , Formatting.Indented);
Sign up to request clarification or add additional context in comments.

7 Comments

I have tried this, and it works for me. (Put quotes on the good, "good")
Error : Cannot perform runtime binding on a null reference
original.data[0].furit_items[0].quality = "good"; - this what I have checked
The name of the your JSON has to be replaced with json in the first line of the answer. Did you do that?
dynamic original = JsonConvert.DeserializeObject(result, typeof(object)); original.data[0].furit_items[0].quality = "good"; var modifiedJson = JsonConvert.SerializeObject(original, Formatting.Indented);
|
1

why you do not want to define the model class ? i totally recommend it, since whatever you do become more safe and easy. http://json2csharp.com/ simply generate the model for you.

if you do not want to use the model at least use json.net - jobject.

2 Comments

couldn't define modal class because it is a dynamic json data
so how can you find the path of adding a new property ?
1

you can use Linq to Json of Json.Net as

JObject obj = JObject.Parse(jsontext);
obj["new_prop"] = "value";//new property as per hirarchy ,same for replacing values
string newjson=obj.ToString();

Comments

0

You can Try like this,it is easy for using javascript code.

data.furit_items.field={"quality":"good"}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.