Skip to content
Advertisement

Convert 2D JS array to json string

My array in variable is:

JavaScript

I’ve mentioned key and value here for the 2D array, I’m trying to convert this to JSON string. I tried JSON.stringify(arraydata), arraydata is the variable where the array is stored, but it makes the string empty, whereas array data is correct.

Edit: This is how I’m adding array data:

JavaScript

Advertisement

Answer

The “array” quoted in your question is not valid JavaScript code. Maybe you had on object of objects instead? In that case the object can easily be converted into a JSON string:

JavaScript

See my comment above. Use objects instead of arrays!

Your corrected script could look something like this:

JavaScript
Advertisement