Create json array from data elements of spans

json array of arrays
sample employee json data
json array of objects
json array of strings
complex nested json example
sample json data
json array of objects java
create a json array

I am new to JSON and I am trying to create a json array that I can send to a php file to process. I want the json to look similar to below.

{"clubs":[
{"number":"12", "type":"break"}, 
{"number":"4", "type":"group"}, 
{"number":"87", "type":"tring"},
{"number":"7", "type":"samestuff"}
]}

I can get the elements into one array but it comes out like this: type:break,number:12,type:group,number:4,type:tring,number:87,type:samestuff,number:7

Can anybody fix this so it creates a json array of the numbers/types? I have attached my current code below.

$(document).ready(function() {
    $("#testbutton").click(function() {
            var clubs = [];
            $(".spanme").each(function() {
            var myarray = [];
            var type = $(this).data("type");
            myarray.push("type:"+type);
            var number = $(this).data("number");
            myarray.push("number:"+number);
            clubs.push(myarray);
            });
        console.log(clubs)
      });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="testbutton">Test button</button>
<span class="spanme" data-number="12" data-type="break">span </span>
<span class="spanme" data-number="4" data-type="group">span </span>
<span class="spanme" data-number="87" data-type="tring">span </span>
<span class="spanme" data-number="7" data-type="samestuff">span </span><br/>

Use querySelectorAll to get all the span with common class & use map which will return an array. Using spread ... will allow to use array methods like map.Get the data attribute by using getAttribute

var x = [...document.querySelectorAll('.spanme')].map(function(item) {
  return {
    "numbers": item.getAttribute('data-number'),
    "type": item.getAttribute('data-type')
  }

})

var someObj = {
  "clubs": x

}
console.log(someObj)
<button id="testbutton">Test button</button>
<span class="spanme" data-number="12" data-type="break">span </span>
<span class="spanme" data-number="4" data-type="group">span </span>
<span class="spanme" data-number="87" data-type="tring">span </span>
<span class="spanme" data-number="7" data-type="samestuff">span </span><br/>

JSON Data Set Sample, Use querySelectorAll to get all the span with common class & use map which will return an array. Using spread will allow to use array methods like map . Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.

You can map the elements into objects for the results.

$(document).ready(function() {
  $("#testbutton").click(function() {
    var result = {
      clubs: $('.spanme').map(function(){
        return { number: this.getAttribute('data-number'), type: this.getAttribute('data-type') };
      }).get()
    };
    
    console.log(result);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="testbutton">Test button</button>
<span class="spanme" data-number="12" data-type="break">span </span>
<span class="spanme" data-number="4" data-type="group">span </span>
<span class="spanme" data-number="87" data-type="tring">span </span>
<span class="spanme" data-number="7" data-type="samestuff">span </span><br/>

jQuery.grep(), the JSON DataSet will create a row for each element in the JSON array, and <​div class="liveSample" spry:region="dsExample1"> Values from array: <span  Like object, record, struct, dictionary, hash table, keyed list, or associative array. An ordered list of values. In various programming languages, it is called as array, vector, list, or sequence. Since data structure supported by JSON is also supported by most of the modern programming languages, it makes JSON a very useful data-interchange

You need to push data into an array clubs. So, first create an object with clubs key and push your value in form of an object to this array. You can create an object using {}.

$(document).ready(function() {
$("#testbutton").click(function() {
    var obj = {'clubs': []};
    $(".spanme").each(function() {
      var type = $(this).data("type");
      var number = $(this).data("number");
      obj.clubs.push({type, number});
    });
    console.log(obj)
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="testbutton">Test button</button>
<span class="spanme" data-number="12" data-type="break">span </span>
<span class="spanme" data-number="4" data-type="group">span </span>
<span class="spanme" data-number="87" data-type="tring">span </span>
<span class="spanme" data-number="7" data-type="samestuff">span </span><br/>

.serializeArray(), If "invert" is true, then the function returns an array consisting of all elements for which "callback" returns false. The $.grep() method removes items from an array as  You can create JSON message data that contains JSON objects, JSON arrays, or both, by creating elements in the logical message tree, under the Data element that is owned by the JSON parser root. A JSON message can have either an anonymous object or an anonymous array as the root of the data.

Append JSON data to jquery div object - JavaScript, serializeArray() method creates a JavaScript array of objects, ready to be encoded as a JSON string. It operates on a jQuery collection of form s Data from file select elements is not serialized. Elements that do not contain a value <p><b>Results:</b> <span id="results"></span></p>. <form>. <select name="​single">. The JSON data is an object (basically an associative array). Indexed arrays use square brackets, [0,1,2], while associative arrays use curly braces, {x:1,y:2,z:3}. Any of the data within the outermost object can be either type of array, but the outermost object itself has to use curly braces.

JavaScript, Hello Folks, Below is sample JSON data: {[ {storename: "abc", loc: "xyz", <div class="pane"> <h3>Test</h3> <p><span class="logo">test</span> 3 mi</p> the values to jquery div object and how can we create jquery div object. A getStoreDetails() function will take one of those arrays, and return the  Read my post again. If you want something to translate into a JSON object, make it an associative array in PHP (where the keys are strings). If you want it to translate into a JSON list, make it a plain array (with implicit integer keys). The value of each array element can in turn be an array, which is what you want. – tdammers Jul 19 '11 at

How to parse JSON in Java, How to convert an Object {} to an Array [] of key-value pairs in JavaScript? ES6 | Promises · How to Create Dark/Light Mode for Website using JavaScript/jQuery? values of all properties owned by enumerables from source objects(one or more) to a target object. Use of JSON is to exchange data to/from a web server. Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.