0
Not a bug

JSON.Stringify

ai-systems@bk ru 8 years ago in iRidium Script updated 8 years ago 4

Good day!

I have a code that create JSON.


var a = 2

var user = {

userName:"admin",

password:"569123A6421D6D392C469CA683504E66",

clientType:"Web3.0",

realm:"Login to 90:02:a9:c4:2b:a3",

random:"Login to 90:02:a9:c4:2b:a3",

passwordType:"Dafault",

id:"2",

session:"a",

}

str = JSON.Stringify(user);

IR.Log(str);


expect to see in log - {"userName":"admin", "password":"569123A6421D6D392C469CA683504E66", "clientType":"Web3.0", "realm":"Login to 90:02:a9:c4:2b:a3", "random":"Login to 90:02:a9:c4:2b:a3", "passwordType":"Dafault", "id":"2", "session":2,


BUT i see - {"userName":"admin", "password":"569123A6421D6D392C469CA683504E66", "clientType":"Web3.0", "realm":"Login to 90:02:a9:c4:2b:a3", "passwordType":"Dafault", "id":"2", "session":2, "random":"Login to 90:02:a9:c4:2b:a3"}


Why order is broken?

Not a bug

You made an error in object creation. In any object you shouldn't add a comma after the last object item.

Please use any JS validator to be sure that you make everything right.


How it should be:

var a = 2;
var user = {
   userName:"admin",
   password:"569123A6421D6D392C469CA683504E66",
   clientType:"Web3.0",
   realm:"Login to 90:02:a9:c4:2b:a3",
   random:"Login to 90:02:a9:c4:2b:a3",
   passwordType:"Dafault",
   id:"2",
   session:"a"
   };
str = JSON.Stringify(user);
IR.Log(str);

and result will be:

{"random":"Login to 90:02:a9:c4:2b:a3", "userName":"admin", "password":"569123A6421D6D392C469CA683504E66", "clientType":"Web3.0", "realm":"Login to 90:02:a9:c4:2b:a3", "passwordType":"Dafault", "id":"2", "session":"a"}

OK!.


Your script copied to iridium 1.0.5


First start
[11-07-2016 22:00:35.999]
INFO SCRIPT {
"random":"Login to 90:02:a9:c4:2b:a3",
"userName":"admin",
"password":"569123A6421D6D392C469CA683504E66",
"clientType":"Web3.0",
"realm":"Login to 90:02:a9:c4:2b:a3",
"passwordType":"Dafault",
"id":"2",
"session":"a"}
[11-07-2016 22:00:36.074] INFO CORE *** The client started to work ***

Second start
INFO SCRIPT
{"userName":"admin",
"password":"569123A6421D6D392C469CA683504E66",
"clientType":"Web3.0",
"realm":"Login to 90:02:a9:c4:2b:a3",
"passwordType":"Dafault",
"id":"2",
"session":"a",
"random":"Login to 90:02:a9:c4:2b:a3"}
[11-07-2016 22:01:31.692] INFO CORE *** The client started to work ***

Third start

[11-07-2016 22:01:44.830] INFO SCRIPT
{"userName":"admin",
"password":"569123A6421D6D392C469CA683504E66",
"clientType":"Web3.0",
"realm":"Login to 90:02:a9:c4:2b:a3",
"passwordType":"Dafault",
"id":"2",
"session":"a",
"random":"Login to 90:02:a9:c4:2b:a3"}

[11-07-2016 22:01:44.906] INFO CORE *** The client started to work ***



Why so?

Hello!


For no obvious reason to us, when you convert the object to a string, the key with name "random" appears in different places. If you change this name to another, all the keys remain in their place. Tell me, how important are the order of the keys in the string, which is obtained after conversion?

Didi you understand why "random" appears in random places of json?