12

This is the json object I am working with

{
    "name": "John Smith",
    "age": 32,
    "employed": true,
    "address": {
        "street": "701 First Ave.",
        "city": "Sunnyvale, CA 95125",
        "country": "United States"
    },
    "children": [
        {
            "name": "Richard",
            "age": 7
        },
        {
            "name": "Susan",
            "age": 4
        },
        {
            "name": "James",
            "age": 3
        }
    ]
}

I want this as another key-value pair :

"collegeId": {
                      "eventno": "6062",
                      "eventdesc": "abc"
                                            }; 

I tried concat but that gave me the result with || symbol and I cdnt iterate. I used spilt but that removes only commas.

concattedjson = JSON.stringify(JSON.parse(json1).concat(JSON.parse(json2)));

How do I add a key pair value to an existing json object ? I am working in javascript.

Aayush
  • 1,244
  • 5
  • 19
  • 48

4 Answers4

13

This is the easiest way and it's working to me.

var testJson = {
        "name": "John Smith",
        "age": 32,
        "employed": true,
        "address": {
            "street": "701 First Ave.",
            "city": "Sunnyvale, CA 95125",
            "country": "United States"
        },
        "children": [
            {
                "name": "Richard",
                "age": 7
            },
            {
                "name": "Susan",
                "age": 4
            },
            {
                "name": "James",
                "age": 3
            }
        ]
    };
    testJson.collegeId = {"eventno": "6062","eventdesc": "abc"};
Jonathan M
  • 17,145
  • 9
  • 58
  • 91
Jobert Enamno
  • 4,403
  • 8
  • 41
  • 63
  • You could simplify the last two lines to just: `testJson.collegeId ={"eventno": "6062","eventdesc": "abc"};` – Jonathan M Jan 11 '13 at 15:32
3

You need to make an object at reference "collegeId", and then for that object, make two more key value pairs there like this:

var concattedjson = JSON.parse(json1);
concattedjson["collegeId"] = {};
concattedjson["collegeId"]["eventno"] = "6062";
concattedjson["collegeId"]["eventdesc"] = "abc";

Assuming that concattedjson is your json object. If you only have a string representation you will need to parse it first before you extend it.

Edit

demo for those who think this will not work.

Travis J
  • 81,153
  • 41
  • 202
  • 273
  • If your `concattedjson` is the same as the OP's, this won't work. His is a string. – Jonathan M Jan 11 '13 at 05:16
  • @JonathanM - Perhaps you should read the link I posted for you. It is a JSON Object, and yes, they do exist. – Travis J Jan 11 '13 at 05:17
  • I get what you're saying in the comment on my answer, and I clarified. Your answer here still won't work on his JSON *string*. – Jonathan M Jan 11 '13 at 05:21
  • the reason this won't work is the OP's `concattedjson` is a *string*. A string is the result of the `.stringify()` method. Your fiddle assumes it's already an object. In fact, your fiddle doesn't involve any JSON at all. It just involves an object. – Jonathan M Jan 11 '13 at 05:25
  • @JonathanM - Here is a second example, where it is a string: http://jsfiddle.net/2bJdg/1/ . – Travis J Jan 11 '13 at 05:27
  • dude, you just did what my answer was (and you said the OP didn't ask about converting the string to an object). – Jonathan M Jan 11 '13 at 05:29
  • The approach I said wouldn't work was using your code without doing the parse on `concattedjson`, which your answer still shows, but your fiddles don't. Man that's all I'm pointing out is that his `concattedjson` is a *string* and you're treating it as an object in your answer. Just correct your answer. – Jonathan M Jan 11 '13 at 05:33
  • There we go. Thank you. Now your answer is the same as mine. – Jonathan M Jan 11 '13 at 05:36
3

Just convert the JSON string to an object using JSON.parse() and then add the property. If you need it back into a string, do JSON.stringify().

BTW, there's no such thing as a JSON object. There are objects, and there are JSON strings that represent those objects.

Jonathan M
  • 17,145
  • 9
  • 58
  • 91
  • The OP did not ask how to convert a string into an object, they asked how to extend the object. Perhaps you should edit your answer. – Travis J Jan 11 '13 at 05:17
  • 1
    @Travis, yes, there *are* JSON objects, but not like the OP is thinking of them. JSON objects are objects created by the JSON library in order to have properties like `.parse()` and `.stringify()`. – Jonathan M Jan 11 '13 at 05:20
0
const newTestJson = JSON.parse(JSON.stringify(testJson));
newTestJson.collegeId = {"eventno": "6062","eventdesc": "abc"};
testJson = newTestJson;
shalonteoh
  • 1,994
  • 2
  • 15
  • 17
  • This is a code-only answer. In general, adding some description explaining what the code does and how it works would be better. Can you please [edit] your answer and add some commentary? Thank you! – Fabio says Reinstate Monica Jun 29 '17 at 14:51