-1

I'm wondering if is there any way to sort a JSON response with Express JS

my JSON file looks like with Postman :

{
"id":"",
"fname":"",
"lname":"",
"country":"",
"phonen":""
}

and I want to change the order and take phonen to the top.

like

   {
    "id":"",
    "phonen":"",
    "fname":"",
    "lname":"",
    "country":""
    }
Stranger B.
  • 9,004
  • 21
  • 71
  • 108
  • 1
    Can you explain why it's important to sort the object by keys without real values associated to them? – Endre Simo Jul 22 '16 at 09:36
  • 1
    And why would Express be the one that needs to solve this? Seems to me like you want to do it with Javascript in general. If you take Express out of the question, you open up a world of possibilities existing already in questions and answers. – Gimby Jul 22 '16 at 09:39
  • 4
    Order of elements in object representation in JSON is udefined and should not be trusted. – Jakub Fedyczak Jul 22 '16 at 09:53

1 Answers1

2

Objects in Javascript does not guarantee order, you would read this relative response.
In bpierre response you could realize

Since ECMAScript 2015, using the Map object could be an alternative. A Map shares some similarities with an Object and guarantees the keys order see the docu

Community
  • 1
  • 1
Manu Artero
  • 9,238
  • 6
  • 58
  • 73