Skip to main content
1-Visitor
November 22, 2021
Solved

Property: Switching JSON

  • November 22, 2021
  • 2 replies
  • 2281 views

Good morning Community!

 

I have set up a service with the input Colour.  The following picture shows the code inside the service. 

 

Bildschirmfoto 2021-11-22 um 10.45.12.png

As shown in the picture I want to send the TimeStamp (ts) first and followed by the Colour which came as input. 

 

The property-value then somehow is the following:

 

Bildschirmfoto 2021-11-22 um 10.45.40.png

 

Why does Thingworx cange the sequence so that there is the Colour first?

 

I need it in the other way, like programmed within the service. Can someone help me with that?

 

Yours faithfully,

Marius

Best answer by bchaudhary_448008

Hi @MS_FLiX_HSD 

 

As we know, JSON is an unordered set of value pairs. But what i have noticed while trying your Use case is that it is sorting values in alphabetical order. So, In case you want your time to come before instead of "ts" name it let's say "wz" or anything which comes after "t" or you can also change type into "abc". 

For example, me.OrderProcess = {"ts":result, "abc": Colour}

 

Revert back if you still face any challenges.

 

Regards

Bhawna

2 replies

16-Pearl
November 22, 2021

I cannot help you with your exact issue. What I would challenge is why you need to have it in the same order? In general a json objects attribute order does not matter as you can access the attribute you want with the attribute name (e.g. me.OrderProcess.type or me.OrderProcess.ts).

 

The json spec also does specify that a json object is an unordered object of key/value pairs - https://stackoverflow.com/a/4920304 

1-Visitor
November 23, 2021

Unfortunately the factory awaits the payload in this order.

5-Regular Member
November 23, 2021

Hi @MS_FLiX_HSD 

 

As we know, JSON is an unordered set of value pairs. But what i have noticed while trying your Use case is that it is sorting values in alphabetical order. So, In case you want your time to come before instead of "ts" name it let's say "wz" or anything which comes after "t" or you can also change type into "abc". 

For example, me.OrderProcess = {"ts":result, "abc": Colour}

 

Revert back if you still face any challenges.

 

Regards

Bhawna

1-Visitor
November 23, 2021

Thank you for this hint! I didn't recognize that its alphabetically. I'll try to change the names. 

16-Pearl
November 23, 2021

I don't think it is alphabetical in any case as when I tested it was not.

e.g. I assigned me.jsonProperty = {"cc": "C","aa": "A","bb": "B"};

and it was saved in the same order.

 

But if you find a working combination good.

 

Otherwise you could also set the property as type string and build the needed format on your own. me.stringProperty = "{\"ts\":\"" + result + ".....}";