0

How to convert javascript Object to Json format?

I can create object data when user submit the form and i dont know how to convert submit data like json format?

can you tell me how to do?

here is my sample code..,

FIDDLE HERE

Example

  "cashpayments": [
{
  "name": something,
  "email": something,
  "password": null
}]                        <<<<< --- LIKE THAT --- >>>>>

(function ($) {
    $.fn.serializeFormJSON = function () {

        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
})(jQuery);

$('form').submit(function (e) {
    e.preventDefault();
    var data = $(this).serializeFormJSON();
    console.log(data);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="#" method="post">
    <div>
        <label for="name">Name</label>
        <input type="text" name="name" id="name" />
    </div>
    <div>
        <label for="email">Email</label>
        <input type="text" name="email" id="email" />
    </div>
    <div>
        <label for="password">Password</label>
        <input type="password" name="password" id="password" />
    </div>
    <p>
        <input type="submit" value="Send" />
    </p>
</form>
Joe
  • 558
  • 1
  • 8
  • 30
  • its not work for me sir – Joe Oct 21 '19 at 09:53
  • 1
    You can try this : https://jsfiddle.net/739up4z5/ – Mohammad Arshad Alam Oct 21 '19 at 09:56
  • i just want to like JSON FORMAT: { "vouchno": 3, "reason": null, "ledgerno": null, "entrytype": null, "vouchdt": null,"cashpayments": [ { "acctcode": 41, "debit": 10000, "credit": null, "narr": "", "actname": "ACCOUNT1" }, { "acctcode": 41, "debit": 10000, "credit": null, "narr": "", "actname": "ACCOUNT1" } ] } – Joe Oct 21 '19 at 10:06
  • What is the issue you are facing in forming that? – Mohammad Arshad Alam Oct 21 '19 at 10:26
  • I'm getting two times data. when i click submit button it will come two times – Joe Oct 21 '19 at 11:34
  • Updated your fiddle for you. Since there's no mention of "cashpayments" in the code/html (only the result), I hardcoded it into the answer, so it's not a generic/reusable method, but gives you want you want using your existing code with a `.push`: https://jsfiddle.net/v91od2m0/ – freedomn-m Oct 21 '19 at 11:43
  • *"I'm getting two times data"* - not in your fiddle or your provided code. So there's some other code you've got that forcing the submit to click twice - running the handler twice maybe? – freedomn-m Oct 21 '19 at 11:45
  • Sir, in this case not work for me, the dropdown datas.. see my fiddle - https://jsfiddle.net/joelshah/ke3htsdm/ – Joe Oct 21 '19 at 12:02
  • @freedomn-m my problem is not getting dynamic table values, when i add multiple rows its not get values.. – Joe Oct 21 '19 at 12:21
  • *None* of that is in your question which asks how to convert to JSON. Perhaps ask a new question with your *actual* problem? – freedomn-m Oct 21 '19 at 12:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201207/discussion-between-joe-and-freedomn-m). – Joe Oct 21 '19 at 12:23

0 Answers0