Typically, a Json object would contain your values (including arrays) as named fields within. So, something like:

JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
// populate the array
jo.put("arrayName",ja);

Which in JSON will be {"arrayName":[...]}.

Answer from david van brink on Stack Overflow
🌐
Boot.dev
boot.dev › blog › javascript › converting an array to a json object in javascript
Converting an Array to a JSON Object in JavaScript | Boot.dev
October 1, 2022 - To get a JSON object from an array with index keys you can use the Object.assign method in conjunction with JSON.stringify.
Discussions

How to convert a JSON array to a JSON object
I used the sqlserver node to query multiple records, and the output is a JSON array, how to wrap this array into a JSON object and then output? I found a lot of nodes but couldn’t achieve it! Please help,thanks! More on community.n8n.io
🌐 community.n8n.io
3
0
April 8, 2025
Convert array to JSON - javascript
I have an Array var cars = [2,3,..] which holds a few integers. I've added a few values to the array, but I now need to send this array to a page via jQuery's .get method. How can I convert it to a JSON object for sending? More on stackoverflow.com
🌐 stackoverflow.com
How to convert JSON object to JavaScript array? - Stack Overflow
This is not the same json structure as the question. This will not work correctly with key/value pairs. It will only return an array of values. 2022-03-07T20:29:53.23Z+00:00 ... @DylanPrem Thankyou for pointing out. Maybe the author edited the question. But anyway, fixed the answer. 2022-03-08T15:16:49.45Z+00:00 ... Theses functions convert any object to ... More on stackoverflow.com
🌐 stackoverflow.com
Converting JSON array to objects
Be careful with this, since it will change the objects in the original array! This might be okay, but it could have unexpected consequences. 2015-08-31T21:56:20.33Z+00:00 ... If you 're going to clone something, better to clone the items of the array instead. (and Array.reduce would work out ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
ConvertSimple
convertsimple.com › home › convert json to javascript object
Convert JSON to Javascript Object - ConvertSimple.com
October 17, 2020 - Paste your JSON input into the left input box and it will automatically convert it into JavaScript Object. The JavaScript Object output is the box to the right.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-json-array-object-to-java-object
How to Convert JSON Array Object to Java Object? - GeeksforGeeks
July 23, 2025 - Below are the steps and implementation to convert JSON Array object to Java object using Jackson library.
🌐
Medium
medium.com › bootdotdev › converting-an-array-to-json-object-in-javascript-f9f1d163cb34
Converting an Array to JSON Object in JavaScript | by Lane Wagner | Boot.dev | Medium
December 28, 2020 - For example: ["apple", "orange", "banana"] // becomes { "0": "apple", "1": "orange", "2": "banana" } To get a JSON object from an array with index keys you can use the Object.assign method in conjunction with `JSON.stringify` following code:
🌐
n8n
community.n8n.io › questions
How to convert a JSON array to a JSON object - Questions - n8n Community
April 8, 2025 - I used the sqlserver node to query multiple records, and the output is a JSON array, how to wrap this array into a JSON object and then output? I found a lot of nodes but couldn’t achieve it! Please help,thanks!
Find elsewhere
🌐
Medium
medium.com › @louistrinh › converting-data-structures-in-javascript-arrays-to-json-objects-fe2cfc53cdf6
Converting Data Structures in JavaScript: Arrays to JSON Objects | by Louis Trinh | Medium
December 12, 2024 - You can create a nested JSON object structure that reflects the 2D array’s dimensions. const twoDArray = [ ["apple", "banana"], ["cherry", "date"], ]; const jsonObj = {}; for (let i = 0; i < twoDArray.length; i++) { jsonObj[`row_${i}`] = twoDArray[i]; } console.log(jsonObj); // Output: { row_0: ["apple", "banana"], row_1: ["cherry", "date"] } ... When converting arrays to JSON objects using indexes as keys, keep in mind that JSON object keys must be strings.
🌐
DEV Community
dev.to › bootdotdev › converting-an-array-to-json-object-in-javascript-3af4
Converting an Array to JSON Object in JavaScript - DEV Community
January 12, 2021 - const array = ["apple", "orange", "banana"] const jsonString = JSON.stringify(Object.assign({}, array)) // jsonString becomes // {"0":"apple","1":"orange","2":"banana"} ... Learn deeper. Build more. Get hired. ... We're a place where coders share, stay up-to-date and grow their careers.
🌐
YouTube
youtube.com › watch
JavaScript Convert Array to JSON Object - YouTube
Converting an array to a JSON object in JavaScript can be easily achieved using the JSON.stringify() method. In this vidoe a tutorial with examples on how to...
Published: December 20, 2023
🌐
InterSystems
community.intersystems.com › post › convert-json-array-list-objects
Convert JSON array to the list of objects | InterSystems Developer Community
December 7, 2023 - However, he ZEN JSON functionality is deprecated in recent versions of IRIS, and the native JSON support is a bit friendlier to use. The documentation is here: https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cl… ... ClassMethod test() { set response = ##Class(%DynamicAbstractObject).%FromJSON(%request.HttpResponse.Data) set iter = response.%GetIterator() while iter.%GetNext(.key,.obj) { ;looping the array object Write "Name: ", obj.Name, !
Top answer
1 of 2
1

You can loop over the elements, and populate a new Object along the way:

var arr = [
    {"id": "01", "state": "Alabama", "category": "Coal", "detail1": null, "detail2": null},
    {"id": "02", "state": "Alaska", "category": null, "detail1": null, "detail2": null},
    {"id": "04", "state": "Arizona", "category": "Oil", "detail1": null, "detail2": null}
];

// Here, I create a copy of the array to avoid modifying the original one.
var obj = {}, copy = JSON.parse( JSON.stringify(arr) );

for(var i in copy){
  obj[ copy[i].id ] = copy[i]; // Add the element to obj, at index id
  delete copy[i].id; // Remove the id from the inserted object
}

console.log(obj);

2 of 2
0

Looping over an array to create a new value typically uses reduce, which allows values to be accumulated in a value that can be passed along to the next invocation of the callback.

I'll assumed in this case that you just want a shallow copy of each member passed in with the id property removed and added to the new value, which can be an object:

var arr = [
    {"id": "01", "state": "Alabama", "category": "Coal", "detail1": null, "detail2": null},
    {"id": "02", "state": "Alaska", "category": null, "detail1": null, "detail2": null},
    {"id": "04", "state": "Arizona", "category": "Oil", "detail1": null, "detail2": null}
]

// Loop over all numeric members of arr
var newStructure = arr.reduce(function(obj, v){

  // Store the value of the ID property of the passed in object
  var id = v.id;
 
  // Remove the id property from the passed in object
  delete v.id;

  // Add the id value and remainder of the object to the accumulator
  obj[id] = v; 

  // Return the accumulator
  return obj;
},{});

document.write(JSON.stringify(newStructure));

Note that this modifies the original objects, the code to copy them instead isn't much longer.

🌐
GeeksforGeeks
geeksforgeeks.org › javascript-convert-an-array-to-json
Convert an Array to JSON in JavaScript | GeeksforGeeks
October 7, 2024 - Any variable that contains pure binary data will be defined in JavaScript as Simple Data, however on some occasions it is sometimes necessary to convert JSON data to an ArrayBuffer, ... In this article, we will learn how to convert an Object to an Array in JavaScript.
Top answer
1 of 3
54

If you have a JS array of JSON objects:

var s=['{"Select":"11","PhotoCount":"12"}','{"Select":"21","PhotoCount":"22"}'];

and you want an array of objects:

// JavaScript array of JavaScript objects
var objs = s.map(JSON.parse);

// ...or for older browsers
var objs=[];
for (var i=s.length;i--;) objs[i]=JSON.parse(s[i]);

// ...or for maximum speed:
var objs = JSON.parse('['+s.join(',')+']');

See the speed tests for browser comparisons.


If you have a single JSON string representing an array of objects:

var s='[{"Select":"11","PhotoCount":"12"},{"Select":"21","PhotoCount":"22"}]';

and you want an array of objects:

// JavaScript array of JavaScript objects
var objs = JSON.parse(s);

If you have an array of objects:

// A JavaScript array of JavaScript objects
var s = [{"Select":"11", "PhotoCount":"12"},{"Select":"21", "PhotoCount":"22"}];

…and you want JSON representation for it, then:

// JSON string representing an array of objects
var json = JSON.stringify(s);

…or if you want a JavaScript array of JSON strings, then:

// JavaScript array of strings (that are each a JSON object)
var jsons = s.map(JSON.stringify);

// ...or for older browsers
var jsons=[];
for (var i=s.length;i--;) jsons[i]=JSON.stringify(s[i]);
2 of 3
7
var json = jQuery.parseJSON(s); //If you have jQuery.

Since the comment looks cluttered, please use the parse function after enclosing those square brackets inside the quotes.

var s=['{"Select":"11","PhotoCount":"12"}','{"Select":"21","PhotoCount":"22"}'];

Change the above code to

var s='[{"Select":"11","PhotoCount":"12"},{"Select":"21","PhotoCount":"22"}]';

Eg:

$(document).ready(function() {
    var s= '[{"Select":"11","PhotoCount":"12"},{"Select":"21","PhotoCount":"22"}]';

    s = jQuery.parseJSON(s);

    alert( s[0]["Select"] );
});

And then use the parse function. It'll surely work.

EDIT :Extremely sorry that I gave the wrong function name. it's jQuery.parseJSON

Jquery

The json api

Edit (30 April 2020):

Editing since I got an upvote for this answer. There's a browser native function available instead of JQuery (for nonJQuery users), JSON.parse("<json string here>")

🌐
Stack Overflow
stackoverflow.com › questions › 61541421 › convert-json-array-to-json-object-python
Convert JSON Array to JSON Object (Python) - Stack Overflow
I have an empty JSON Array... FINAL_ELOGII_DATA_ARRAY = [] That gets populated via .append inside a for loop from a Python Object... for ord in FINAL_SHOPIFY_DATA: FINAL_ELOGII_DATA_ARRAY.ap...
🌐
Make Community
community.make.com › questions
How do I convert an array to a JSON? - Questions - Make Community
November 25, 2024 - Hi folks, Ok the title is a bit simplistic, but I’m so close to solving something and I just can’t seem to get the final piece done. I’m encountering an issue while trying to build a JSON payload in the Create JSON module. Despite correctly formatting the data in a previous module, the value for one of my keys (SalesEntryLines) is being transformed into [Collection], [Collection] instead of the expected JSON array.