0

I have to form a json output using jquery. What needs to be represented with this json message is a collection of items named 'bucket'. The collection will include image elements, link elements, term elements or video elements. How many items will exist in each category of elements is not pre-specified, but rather dynamic. Before proceeding with jquery I decided the json output to look like the following without being sure that this is correct.

{ 
    "bucket": {
        "terms": [ {
            "text": "logo design",
            "data": "Interview with Petter Johansson at Stockholm Design Week"
        } ],
        "images": [{
            "title": "Tattoo Design",
            "thumbnail": "http://ts2.mm.bing.net/th?id=HN.608019252933102717&pid=15.1",
            "url": "http://www.deerydesign.net/wp-content/uploads/2013/03/Tattoo-Design-006.jpg"
        }],
        "links": [{
            "title": "Design News � Official Site",
            "link": "http://www.designnews.com/",
            "snippet": "Design News is the leading technical resource for design engineers and engineering managers who build products and systems through application of electronics & test ... "
        }],
        "videos": [{
            "title": "Design Projects done in less than a minute each ",
            "thumbnail": "https://i.ytimg.com/vi/vwtOGBxtI10/mqdefault.jpg",
            "url": "http://www.youtube.com/watch?v=vwtOGBxtI10"
        }]
    }
}

After that I need to form json message through jquery, but I find difficulty in doing so because of the multiple levels and the non-fixed number of items in each element's array. I started somehow like this, but I don't know how to proceed with next level.

jsonObj = {
        bucket: []
    };
jsonObj.bucket.push({
            "terms" : $(data).text()
        });

Any help is appreciated.

1 Answer 1

1

jsonObj.bucket is an object, not an array, so you can't call push on it. The array is in terms, so it should be:

jsonObj = {
    bucket: {
        terms: [],
        images: [],
        links: [],
        videos: []
    }
}

Then you add to it with something like this (I'm just making up expressions for where the data comes from, since you haven't given enough information for me to know it):

jsonObj.bucket.terms.push({
    text: $(somefield).text(),
    data: $(otherfield).val()
});
Sign up to request clarification or add additional context in comments.

Comments

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.