0

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

1 Answer 1

3

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Wow that is very simple. Thank you for the fast, easy, and nice answer :)

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.