4

I have a rather large JSON object being generated via PHP. It creates a PHP object out of a database, with keys that are integers, ie 1-100. These keys are not in that order though, they are in a random order, such as 55, 72, 5, 8, 14, 32, 64, etc. I then use json_encode to output the object as JSON. I then use an AJAX call to grab that JSON and store it in a variable. However, that variable has the JSON object in order 1-100, instead of the sorted order above.

Any ideas why it's doing that, and how I can fix it?

1
  • Please improve this question by showing some code. Commented Dec 4, 2017 at 23:19

2 Answers 2

5

JSON objects have no specific order and are not guaranteed to keep the order you put things in. If you want keys in an order, you should put them in an array which will maintain order. If you want an ordered set of key/value pairs, you can use several different forms.

The simplest would be to just have a single array that is an alternating set of key, value:

var data = ["key1", "value1", "key2", "value2",...]; 

Or, you could do an array of objects:

var data = [{key: "key1", data: "value1"}, {key: "key2", data: "value2"}, {...}]
Sign up to request clarification or add additional context in comments.

5 Comments

My Object originated as just an index (1-100) with several values, including the ID, and even a few nested objects/arrays. The problem is that it was requiring my code to store too many attributes in elements in the DOM. Instead I thought I would make the index the id so I could call object[id].name, etc. However, I need the information to generate objects that are appended to an element. That is why maintaining the order is important.
Actually, your original problem is pretty interesting. You should post a SO question about that.
@sharf What do you mean it required too many attributes in elements? I don't think there's a practical limit.
@PatrickGunderson No, there wasn't a limit. The issue is that searching and matching by attributes was becoming tedious and thought it would be nicer to reference the object for all info, and just need to know the ID as I go.
searching and matching by attributes is tedious! Have you looked into using any helper libraries that have functions for this type of thing built-in?
2

Keys within an object aren't technically ordered. That you expect them to be in a particular order is a mistake in data structure. A JSON parser will interpret numerical keys as array positions.

this

{
    55: "foo",
    29: "bar",
    ...
}

is semantically the same as:

object[55] = "foo"
object[29] = "bar"

which is:

[
    ...
    "bar" //index 29
    ...
    "foo" //index 55
    ...
]

Instead, you should separate the order and the identify of your objects:

[
    {id: 55, content: "foo"},
    {id: 29, content: "bar"},
    ...
]

20 Comments

The last part "use an array" is repetitive. It could be omitted, as the previous example is an array of objects. A good alternative would be to teach him how about Array.sort() to put the array of objects in whatever order he would like when it is out of order.
The array of objects is an order-indeterminant collection, while the final array is ordered. I'll edit to make that clear.
Any Array in Javascript is ordered. It is Object that contains key/value pairs and is unordered. Every Array has an integer index.
You are correct. In this case the order of insertion of objects will be preserved, as will the OPs id numbers.
I think this might be the solution I'm looking for but I'm not sure how to make that work. My object already has the id as key/value in each one. The problem is I loop through the object appending to an element and need the object in the right order before hand.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.