how to create array of json object without key in javascript

Related searches

I want to create an array of JSON object without a key.How can this is achieved ..??

for example [{8,0,2}, {20,0,2}].

var hh = 9
var mm = 8
var qty = 2
var data = [];

it gives data like [{hh:9,mm:8,qty:2}] I want array like [{9,8,2},{9,3,4}]

Without key value pair object was not created .so that's why its adding key name from variable name

see this you error.Its a invalid one

var a = [{8,0,2}, {20,0,2}];

Create JsonArray without key value, JSON objects are written in key/value pairs. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). 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't.

Object Literal Property Value Shorthands allow you to create an object where the property names are inferred from the variable names you use to pass the data into.

If you don't have variable names, then there is nothing for the JS engine to use to figure out what the property names should be.

Consider using a function instead.

console.log([time(8,0,2), time(20,0,2)]);

function time (hh, mm, qty) {
    return {hh, mm, qty};

JSON Objects, They can even be nested by consisting of objects with an array of objects with an array of Strings or worse, some more objects and arrays. Stringify a JavaScript Array. In JSON, date objects are not allowed. function will remove any functions from a JavaScript object, both the key and the value:

The result you get at the end makes sense. By doing {hh,mm,qty} you are effectively saying "Use the variable name as the key and its value as the value". It might help us more if you provide an example of how you intend to use the result and access the variables i.e. the shape of the object you want in the end.

That being said, there are a couple alternatives:

Using values as the keys

If you really wanted your object to look similar to {8,0,2} you could use those values as the keys (all keys get converted to strings anyways) so you could do the following:

var example1 = {8:undefined,0:undefined,2:undefined};
var example2 = {8:null,0:null,2:null};

var keys = [];
for(var name in example1) {
// keys = ["8","0","2"];

var otherKeys = Object.keys(example2);
// otherKeys = ["8","0","2"];
Setting the keys dynamically
var hh = 9;
var mm = 8;
var qty = 2;

var obj = {};
obj[hh] = null;
obj[mm] = null;
obj[qty] = null;

//obj = {9:null,8:null,2:null};

I'm not certain if this solves your problem or answers your question entirely but it might give you some more insight into what is happening and why. The above examples are a common way to create a quick lookup versus a dictionary with would have values in place of null or undefined.

Parsing JSON Without Keys. So you've just made a call, an API , I don't get what are JSONArray and JSONObject, you haven't shown their code. Since you wrote you make your own parsing, I supposed to stored the names of the keys? no, because my php parser and javascript parser,� Converting a JSON Text to a JavaScript Object. A common use of JSON is to read data from a web server, and display the data in a web page. For simplicity, this can be demonstrated using a string as input. First, create a JavaScript string containing JSON syntax:

You example uses a new feature of ECMAScript 6 that is the shorthand syntax for initialising object properties. This line in your example:


is equivalent to this verbose one:

data.push({hh: hh, mm: mm, qty: qty});

An object in JavaScript will always have keys and values. There is no way to save just values in a plain object. However, there are two other solutions.

One is using an array:

data.push([hh, mm, qty]);

Note the square brackets substituting the curly ones. This will obviously push an array of three values onto the data array. When retrieving the values, you can just refer to their index, as an array's items will always retain their indices:

var data2 = [hh, mm, qty];
var hh2 = data2[0];
var mm2 = data2[1];
var qty2 = data2[2];

Another way of just "saving the values" is using a set, though the construction of a Set object will still require passing it an array:

data.push(new Set([hh, mm, qty]));

Accessing the data is less straightforward in this case, as the set will typically only let you iterate it. Unlike similar data structures in other languages, a JavaScript set will retain the order of inserted values. It can therefore be safely converted into an array:

var mySet = new Set([hh, mm, qty]);
var data3 = Array.from(mySet);
var hh3 = data3[0];
var mm3 = data3[1];
var qty3 = data3[2];

You can read more about sets here.

iterating thru jsonobject without key names, There's no value in randomly naming the keys in your objects; the objects should be a standard form. [{key:"ABC",question:"Question: ABC is� Dynamically creating keys in JavaScript associative array; Creating a JavaScript array with new keyword. Prefix sums (Creating an array with increasing sum) with Recursion in JavaScript; MongoDB query to find data from an array inside an object? Creating SAP interface to pull data from web application; How to convert an array to JSON Array

You can wrap it over another JSON object with a key I assume you want a JSON object.

Like this { [{8,0,2}, {20,0,2}] } but this with a problem - It is not a valid JSON.

I had a similar problem for one of my scenario. Then I realised

A top level JSON can't exist without a key!

Consider this example, you have another KV pair in JSON and also this array.

  "somekey" : "somevalue",
  [ {8,0,2}, {20,0,2} ]

You can fetch "somevalue" with the key "somekey". But how would you access the array? you can't :(

I would suggest you to use a top level key for the JSON and make this array as value of it's. Example:

  "my array" : [ {8,0,2}, {20,0,2} ]

Accessing JSON Array Data Without Knowing Its Keys, On average I work with JSON data 18 times a week. And I still need to google for specific ways to manipulate them almost every time. What if there was an ultimate guide that could always give you the answer? In this article, I'll show you the basics of working with object arrays in JavaScript. If you ever worked with a JSON structure, you've worked with JavaScript objects. Quite literally

Notice how each item in the new object uses the array index of the value as the key (i.e. "0", "1", etc.). And you can now access each item in the new object using the conventional dot notation method ( In the next section, we'll show you how to manually loop over an array to construct a new object. 2. Loop Over Array & Construct a New

Syntax Object.keys(obj)Parameters obj The object of which the enumerable's own properties are to be returned. Return value. An array of strings that represent all the enumerable properties of the given object.

Comparison to JavaScript Object. It is worth keeping in mind that JSON was developed to be used by any programming language, while JavaScript objects can only be worked with directly through the JavaScript programming language. In terms of syntax, JavaScript objects are similar to JSON, but the keys in JavaScript objects are not strings in quotes.

  • the wanted result is not a valid javascript object.
  • There's no such thing as an object without keys. An array is an object with implicit keys, i.e. you don't have to care about them. So just use an array.
  • Do you mean ` [[9,8,2],[...]]`?
  • You can refer this json-object-without-keys
  • arrays are basically objects too. what you seek for is an array. not an object.
  • console like this [ { hh: 8, mm: 0, qty: 2 }, { hh: 20, mm: 0, qty: 2 } ]
  • No, I want as integer array type
  • @VedPrakash Then the answer is simple, instead of using a standard object use an Array and initialize like so: [8,0,2] or [hh,mm,qty] substituting the { } for [ ]. :-)