I know that I can create this JSON:

[
    {
        "Title": "Something",
        "Price": "234",
        "Product_Type": "dsf sf"
    },
    {
        "Title": "hskiuea",
        "Price": "4234",
        "Product_Type": "sdawer"
    }
]

*It uses the values obtained from text inputs contained within an element with the class "newpappend" - As shown below

Using the following code which obtains values from my HTML:

var jsonObj = []; //declare array


$(".newpappened").each(function () {

    var p_title = $(this).find('#p_title').val();
    var p_price = $(this).find('#p_price').val();
    var p_ptype = $(this).find('#p_ptype').val();

    jsonObj.push({Title: p_title, Price: p_price, Product_Type: p_ptype});
    $(this).remove();
});

But, my goal is to end up with the JSON structured like this:

{
    "Product_List": {
        "Product": [
            {
                "Title": "asdf",
                "Price": "53",
                "Product_Type": "Adfsdf"
            },
            {
                "Title": "asgsd",
                "Price": "123",
                "Product_Type": "Ntohig"
            }
        ]
    }
}

Basically, I am struggling with the correct Javascript to use to reach my goal

link|improve this question

feedback

1 Answer

up vote 3 down vote accepted

You are really close. Start with what you have, and then later:

var output = {
    "Product_List": {
        "Product": jsonObj
    }
}
// output is now what you are looking for.
link|improve this answer
Wow that is very simple. Thank you for the fast, easy, and nice answer :) – TaylorMac Feb 23 at 5:51
Will accept in 5 – TaylorMac Feb 23 at 5:51
And then JSON.stringify(output) if you actually want JSON and not an object... – nnnnnn Feb 23 at 6:05
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.