Soy nuevo en JSON y probé el siguiente ejemplo para ver los resultados, pero devuelve una matriz vacía en la consola. ¿Alguna sugerencia?
function createJSON() { var obj = []; var elems = $("input[class=email]"); for (i = 0; i < elems.length; i += 1) { var id = this.getAttribute('title'); var email = this.value; tmp = { 'title': id, 'email': email }; obj.push(tmp); } var jsonString = JSON.stringify(obj); console.log(jsonString); } createJSON(); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>El problema con su código es porque está mezclando métodos simples de JS y jQuery. Por ejemplo, no debe iterar a través de un objeto jQuery con un bucle for , y un objeto jQuery no tiene un método getAttribute() . Usaría each() y attr() o prop() en esos casos, respectivamente.
Dicho esto, puede simplemente crear una matriz a partir de un objeto jQuery que contenga una colección de elementos usando map() , algo como esto:
function createJSON() { let arr = $('.email').map((i, el) => ({ title: el.title, email: el.value })).get(); return JSON.stringify(arr); } let json = createJSON(); console.log(json); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <input type="email" class="email" title="email_1" value="foo@foo.com" /> <input type="email" class="email" title="email_2" value="bar@bar.com" />