0

I would like to create a dynamic JSON array in Javascript like that :

var jsonData = {
    "Tasks" : [
        {
            "__metadata" : {
                "uri" : "...",
                "type" : "..."
            },
            "ID" : "...",
            "Category" : "..",
            // ...
        },
        {
            "__metadata" : {
                "uri" : "...",
                "type" : "..."
            },
            "ID" : "...",
            "Category" : "..",
            // ...
        },
        // others tasks
    ], "__count" : "2"
};

I found how to create a JSON in Javascript but not with this syntax. So what is the good way to create the JSON object with the 'Tasks' array and add items in it ?

Thank you for your help

6
  • What do you mean by "dynamic"? Commented Mar 11, 2013 at 9:50
  • Does this help: stackoverflow.com/questions/4314008/… Commented Mar 11, 2013 at 9:52
  • your question doesn't make sense. you already have the javascript you want, all you need to call is JSON.stringify() on it and you're done. Commented Mar 11, 2013 at 9:53
  • Also the __count attribute is redundant. It is already stored within the Tasks array. Commented Mar 11, 2013 at 9:56
  • This is an object literal, not JSON. Please use the correct terminology, otherwise it is difficult to understand your problem. But I believe the answer is: you can't. You can create the initial object with an object literal and then add the data to the array later. Commented Mar 11, 2013 at 10:01

3 Answers 3

1

Something like this maybe?

function Metadata(uri,type)
{
    this.uri = uri;
    this.type = type;
}

function Task(metadata, id, category)
{
    this.__metadata = metadata;
    this.ID = id;
    this.Category = category;
}

var tasks = [new Task(new Metadata('foo','bar'), 1, 'blah'), new Task(new Metadata('foo1', 'bar1'), 2, 'blah2')];

document.write(JSON.stringify({"Tasks": tasks, "__count": tasks.length}));
Sign up to request clarification or add additional context in comments.

Comments

1

Create objects with constructors and use the JSON api.

var jsonData = {};
jsonData.Tasks = new Array();

var Metadata = function(uri, type){
  this.uri = uri;
  this.type = type;
}

var Task = function (metadata, id, category){
  this.__metadata = metadata;
  this.ID = id;
  this.Category = category;
}

var tasks = jsonData.Tasks;

tasks.push(
  new Task(
    new Metadata("someUri", "someType"),
    1,
    "someCategory")
);

jsbin: working example

Comments

0

I'm using this code and it works :

var jsonData = {};
jsonData.Tasks = [];

var count = 0;

for(...) {
    jsonData.Tasks.push({
        "__metadata" : {
            "uri" : "...",
            "type" : "..."
        },
        "ID" : "...",
        "Category" : "...",
    });

    count++;
}

jsonData.__count = count;

Thank you for your answers and your reactivity !

1 Comment

I still recommend you use objects rather than string literals. I don't know what it means performance whise, but is a lot less error prone.

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.