0

I am new to JSON and tried the below example to see the results but it returns an empty array in the console. Any suggestions?

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>

3
  • Change $("input[class=email]") into $('input[class="email"]'). You need to add quotes for attributes values in a selector. And since it's a class, you can use $('input.email'). Commented Oct 13, 2021 at 17:55
  • 1
    @Gil not quite accurate. The quotes around attribute values in a selector are only required if the value contains a space. In this case [class="email"] and [class=email] will both work - although .email would be far more performant and better practice. Commented Oct 13, 2021 at 18:16
  • changed but still getting this output in console [] , its empty Commented Oct 13, 2021 at 18:24

1 Answer 1

1

The issue with your code is because you're mixing plain JS and jQuery methods. For example, you shouldn't iterate through a jQuery object with a for loop, and a jQuery object doesn't have a getAttribute() method. You would use each() and attr() or prop() in those cases, respectively.

That said, you can more simply create an array from a jQuery object containing a collection of elements using map(), something like this:

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="[email protected]" />
<input type="email" class="email" title="email_2" value="[email protected]" />

Sign up to request clarification or add additional context in comments.

3 Comments

var networkgroup_obj="GRP1"; var NetworkObjectUUID1="ABC"; var NetworkObjectUUID2="XYZ"; I am trying to create for a json dynamic function for this but bit not clear how to???? [{ "name": "networkgroup_obj1", "objects": [ { "type": "Host", "id": "NetworkObjectUUID1" }, { "type": "Host", "id": "HostObjectUUID2" }}
That's a much more complex nested structure than your question states. I would suggest you start a new question about that, making sure to include all the relevant HTML along with an accurate example of the output you want to create.
Thank you very much, i found a way to get complete string , i just add that fix prefix to json output using php string append function and it worked , anyway its not a a very effienct way to do, but it solved the problem for now.

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.