5

In Javascript I try to use stringify but it keeps returning an empty string. What is wrong here? Feel free to edit the Fiddle.

JS

values = [];
values['belopp'] = 2322;
values['test'] = 'jkee';

str = JSON.stringify(values);

console.log(values);
console.log(str); // Expected to show a json array

JS Fiddle

https://jsfiddle.net/L4t4vtvd/

Jens Törnell
  • 23,180
  • 45
  • 124
  • 206

2 Answers2

9

You are trying to use something that is meant for an object on an array.

values = {};
values['belopp'] = 2322;
values['test'] = 'jkee';

str = JSON.stringify(values);

This is the updated fiddle.

vjdhama
  • 4,878
  • 5
  • 33
  • 47
4

You are stringifying an array ([]), not an object ({}) Therefore, values = {};

Kutyel
  • 8,575
  • 3
  • 30
  • 61