NitZRobotKoder
NitZRobotKoder

Reputation: 1096

Constructing JSON in Javascript

I am trying to construct JSON object in js. There are couple of posts already about this topic in stack overflow itself. Referred to How do i build JSON dynamically in javascript?. Have to construct JSON exactly something like mentioned in the post.

{
    "privilege": {
        "accesstype": "VIEW",
        "attribute": [
            {
                "code": "contenttype",
                "displayname": "Content type",
                "value": {
                    "valcode": "book_article",
                    "valdisplayName": "Book Article"
                }
            },
            {
                "code": "mime",
                "displayname": "Mime type",
                "value": {
                    "valcode": "xml",
                    "valdisplayName": "Xml"
                }
            }
        ]
    }
}

Follwed the answers in the post and tried this,

var privilege = {};

privilege.attribute[0].code = "contenttype";
privilege.attribute[0].displayname = "Content type";
privilege.attribute[0].value.valcode = "book_article";
privilege.attribute[0].value.valdisplayName = "Book Article";

But getting error as privilege.attribute undefined.

I am not able to figure out where I am going wrong. Assuming there must be some declaration problems. Any light on it would be of great help.

Upvotes: 4

Views: 19594

Answers (3)

devOp
devOp

Reputation: 3180

Try this:

 var privilege = {};
 privilege.attribute = [];
 privilege.attribute[0] = {};
 privilege.attribute[0].code="contenttype";
 privilege.attribute[0].displayname="Content type";
 privilege.attribute[0].value = {};
 privilege.attribute[0].value.valcode="book_article";
 privilege.attribute[0].value.valdisplayName="Book Article";

Have a look at Javascript Object. There is a lot of stuff out there. E.g http://mckoss.com/jscript/object.htm

Edit: Initialized privilege.attribute[0] = {}; after hint in comment.

Upvotes: 9

Jim Blackler
Jim Blackler

Reputation: 23179

Why not just do

var privilege = {
    "accesstype": "VIEW",
    "attribute": [{
        "code": "contenttype",
        "displayname": "Content type",
        "value": {
            "valcode": "book_article",
            "valdisplayName": "Book Article"
        }
    }, {
        "code": "mime",
        "displayname": "Mime type",
        "value": {
            "valcode": "xml",
            "valdisplayName": "Xml"
        }
    }]
}

... in fact, you don't need the keys to be strings, you could write...

var privilege = {
    accesstype: "VIEW",
    attribute: [{
        code: "contenttype",
        displayname: "Content type",
        value: {
            valcode: "book_article",
            valdisplayName: "Book Article"
        }
    }, {
        code: "mime",
        displayname: "Mime type",
        value: {
            valcode: "xml",
            valdisplayName: "Xml"
        }
    }]
}

Upvotes: 1

Salman
Salman

Reputation: 9447

Try this

privilege.attribute = [];
privilege.attribute.push({});

privilege.attribute[0].code="contenttype";
privilege.attribute[0].displayname="Content type";
privilege.attribute[0].value.valcode="book_article";
privilege.attribute[0].value.valdisplayName="Book Article";

Upvotes: 0

Related Questions