Skip to content

Instantly share code, notes, and snippets.

@melvinstanly
Created May 3, 2021 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melvinstanly/e59ba97571ca7fb05a888b356524ee81 to your computer and use it in GitHub Desktop.
Save melvinstanly/e59ba97571ca7fb05a888b356524ee81 to your computer and use it in GitHub Desktop.
JSON.stringify returns []
function json_encode(){
var content = [];
content.field1 = { width: "100px", height: "400px", label : "Width"};
content.field2 = { width: "200px", height: "500px", label : "Height"};
console.log(JSON.stringify(content));
}
// The above function return [] because the key used here is a string.
//The array key should be integer for JSON.stringify to return proper value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment