1

I am new to JSON and Here is my First JSON Object

var First = {
              "a" : [{}]
            };

I want to add the below object to "a" in "First"

var a = {"1":"One","2":"Two"};

I have tried below code

First.a[First.a.length-1] = a;

It is not working.I assume that there are some syntax mistakes in this. Please help me on this.

2
  • 2
    I removed the tags jquery and json, as neither is at all relevant to this question, and added javascript. Commented Dec 29, 2011 at 15:05
  • Please review json.org for a better understanding of what JSON is and how it works. Commented Dec 29, 2011 at 15:58

3 Answers 3

1

it you want to add it, you're looking for First.a.push(a) if you want to replace the last element:

First.a[First.a.length-1] = a;

if you want to append a to the last element:

First.a[First.a.length-1]['a'] = a;

or

  First.a[First.a.length-1].a = a;

if it's none of these, please add the expected json in your question.

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

8 Comments

OMG, Can we push as we push in Arrays?
@Sol, a is an array, so you can use push. [] is the array literal syntax, {} is the object literal syntax.
Is it ironic or I don't get the question? edit: ok your problem was you didn't see you were in an array :)
@Guillaume86 How to retrieve element of pushed object("a" in the above case).
actually you got the get synthax, just use the same to do the set ;)
|
1

You should use Array.push().

var myObject = { 
    myArrayOfObjects: []
};

var newObject = { 
    1: '1',
    2: '2'
};

myObject.myArrayOfObjects.push(newObject);

2 Comments

push will do always bottom of you existing array, how to add item in top of the existing array?
1

That's not a "JSON object". There isn't even any such thing. It's a Javascript object.

It's not working as you expect as you are not adding an item to the array, you are replacing the last item.

Just use the push method to add an item at the end of the array:

First.a.push(a);

14 Comments

I thought JSON object (though redundant) was the same thing as javascript object?
@jbabey: It's not. JSON is a text format that is mainly used to send data to (or from) Javascript client code. It can however be used in an environment where there is no Javascript involved at all.
@Guffa, I have to disagree. JSON stands for JavaScript Object Notation. a JSON object (an object that uses JavaScript Object Notation as defined at json.org) uses the literal syntax: {}, a JSON array uses the literal syntax: []. In other languages it may need to be encoded as a string, but in JavaScript, it's simply the common syntax for instantiating objects/arrays, although it can also be stored in a string format.
I personally think it is just a semantic difference, but I think what Guffa is trying to say is that it is not a "JSON object", but rather a javascript object that happens to contain JSON, which is a text format that provides a standard way to represent data.
@zzzzBov: Just because JSON borrows part of the syntax from Javascript doesn't mean that it is Javascript. If you use the syntax in Javascript code, then it's Javascript, not JSON.
|

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.