0

I have JSON in Javascript like this:

var json = {
    "total": "100",
    "page":"1",
    "records":"100",
    "rows": [
        {
            "no": "1",
            "part_number": "2",
            "part_name": "3",
            "price": "4",
            "note": "8"
        }
    ]
};

and I want to add

"test1":"5",
"test2":"7"

into JSON above.

so it will be like this:

var json = {
    "total":"100",
    "page":"1",
    "records":"100",
    "rows": [
        {
            "no": "1",
            "part_number": "2",
            "part_name": "3",
            "price": "4",
            "test1": "5",
            "test2": "7",
            "note":"8"
        }
    ]
};
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
Rio Eduardo
  • 233
  • 2
  • 4
  • 8
  • 3
    You have an object literal, not JSON. [Learn more about objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects). Also have a look at this question: [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json). – Felix Kling Jun 20 '13 at 14:44

2 Answers2

2
json["rows"][0]["test1"] = "5";
json["rows"][0]["test2"] = "7";
go-oleg
  • 19,272
  • 3
  • 43
  • 44
2

Just:

json.rows[0].test1=5; // or if you want "5"
json.rows[0].test2=7;
Thomas Junk
  • 5,588
  • 2
  • 30
  • 43