Question: I'm trying to setup a form where some fields needs to be hidden depending on the user role. I'm doing this in my own module using hook\_form\_FORM\_ID\_alter. No problem with common text, email or link fields (e.g. $form['field\_companyname']['und'][0]['value']['#type']='hidden'). But for an image field or a multiple value file field the usual way won't work.

Anybody can give me a clue?

Answer: 
I just come to know when I press *ctlr+space bar*, in which its clearly written that behavior of the *keys* is undefined, orders is not maintain by *keys.*

![enter image description here](https://i.stack.imgur.com/3mIfZ.png)

*Arun George* said@ correctly that you have to use any sorting method to achieve your goal.

and for sorting may be this [link](https://stackoverflow.com/questions/7051785/when-jsonobject-keys-are-iterate-it-is-not-in-the-same-order-as-in-response-from) will help you.
Use different library to parse json dynamically. 

Below I wrote a piece of code based on Jackson JSON Processor, which is the best JSON library in my opinion

```
public void test() throws IOException {
    String str = "{\n" +
            "  \"result\": \"Success\",\n" +
            "  \"AlertDetails\": [\n" +
            "    {\n" +
            "      \"ShipmentNumber\": \"SHP34\",\n" +
            "      \"Customer\": \"BEST\",\n" +
            "      \"DateCreated\": \"2012-08-29T04:59:18Z\",\n" +
            "      \"CustomerName\": \"BEST\"\n" +
            "    }\n" +
            "  ]\n" +
            "}";

    JsonFactory factory = new JsonFactory();
    JsonParser jsonParser = factory.createJsonParser(str);

    JsonToken jsonToken;
    SerializedString alertDetails = new SerializedString("AlertDetails");
    while (!jsonParser.nextFieldName(alertDetails)) { /* move to AlertDetails field */ }

    jsonParser.nextToken(); // skip [ start array
    jsonParser.nextToken(); // skip { start object
    // until } end object
    while ((jsonToken = jsonParser.nextToken()) != JsonToken.END_OBJECT) {
        if (jsonToken == JsonToken.FIELD_NAME) {
            System.out.println(jsonParser.getCurrentName());
        }
    }

}

```

It simply prints out field names in the same order as in json:

```
ShipmentNumber
Customer
DateCreated
CustomerName

```

---

**EDIT**

Naturally you can use other libraries like gson etc. But remember, as is written on [json.org](http://json.org), that:

> 
> An object is an unordered set of name/value pairs.
> 
> 
> 

and the order of keys depends on implementation and might vary in each request.