1
var data = {};
$('.table-row').each(function(i, el) {
   var key = $(el).find('.key').val();
   var value = $(el).find('.value').val();
   data[key] = value ;
}
var json = JSON.stringify(data);
// output unordered JSON string, since associative object does not preserve order
console.info(json);

How to create ordered JSON string where keys are ordered in insertion order?

For example, when table-row contains elements in following order - a, x, c I need receive JSON string like this {a: 1, x:2, c: 'value001'}

user12384512
  • 3,362
  • 10
  • 61
  • 97
  • 1
    Possible duplicate of [Keep the order of the JSON keys during JSON conversion to CSV](https://stackoverflow.com/questions/4515676/keep-the-order-of-the-json-keys-during-json-conversion-to-csv) – imtheman Aug 09 '18 at 21:27
  • 2
    Why do you need this? – Felix Kling Aug 09 '18 at 21:28
  • @imtheman how this question is related to original question? java vs javascript, different language, different issues – user12384512 Aug 09 '18 at 21:31
  • @FelixKling I'm sending arbitrary data to server (associative array) and I need to preserve the order of the array. It looks for me the easiest way to do it - serialize it into JSON – user12384512 Aug 09 '18 at 21:45
  • 1
    Even if you could order the properties in the JSON correctly, there is no guarantee that the server side language will preserve the order (you may switch to a different language in the future). If you absolutely need order, use an array. – Felix Kling Aug 09 '18 at 21:47
  • 1
    This really sounds like an XY problem – charlietfl Aug 09 '18 at 21:48
  • Are we talking about DOM traversal order here? – ibrahim tanyalcin Aug 09 '18 at 22:01
  • @ibowankenobi in the example above the order of the keys in JSON string should be same that was the DOM traversal order. But instead of DOM traversal order it can be iteration over ordered array – user12384512 Aug 09 '18 at 22:12
  • @FelixKling anyway I'm agree with you that JSON is not designed for ordered iteration no matter what programming language is used. So maybe it makes sense to close the question – user12384512 Aug 09 '18 at 22:13
  • 1
    1 question, why didn't you want to use an array of arrays? [[k,v],[k,v]...] etc? – ibrahim tanyalcin Aug 09 '18 at 22:19
  • It seems that you've tricked yourself. Telling by the discussion here, your data structure ain't any associative array consisting of key-value pairs, but instead it is an ordered list of items that just happen to have properties called `key` and `value`. At least some part of the association of values seems to be made in by the index *(position)* and not just by the `key`. Maybe you should change how you look at/percieve your data – Thomas Aug 09 '18 at 22:24
  • You could instead store an array of keys and an array of values, something like `'table-row_keys': table-row.keys` and similar for values. Then when parsing the json you just have to combine them again. A bit of extra work, but would maintain your order. – obermillerk Aug 09 '18 at 22:34
  • @Thomas it's typical hash map where entries are ordered by insertion order. Very common data structure in multiple languages. In Java it's called LinkedHashMap – user12384512 Aug 11 '18 at 22:27

0 Answers0