cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

Unable To Convert From com.thingworx.types.collections.ValueCollectionList to JSON

rad1
9-Granite

Unable To Convert From com.thingworx.types.collections.ValueCollectionList to JSON

Unable To Convert From com.thingworx.types.collections.ValueCollectionList to JSON

Within my service ParentService (with output type set to InfoTable) I call ChildService service which returns  result: INFOTABLE dataShape: "undefined" and I set result to that InfoTable so this can be converted to JSON so a Rest client can ask for JSON response.

This "undefined" Infotable when converted to JSON will have this shape:

 

{
  "dataShape": {...},
  "rows": [{firstobj}, {seconobj}...]
}

I want to return only result.rows, but that is treated as ValueCollectionList which cannot be converted to JSON.

 

 

I used  InfoTableFunction's ToJSON service and I got almost what I wanted with .rows, but now I need to remove{ "array": part and only get this as a return object:

[{obj1},{obj2},{obj3}]. I want then to save this json return value to a repository file. So how can I do that?

It is a strange that "array" property is added. I don't want to have a wrapping object with this "array" property.

 

var jsonResult = Resources["InfoTableFunctions"].ToJSON(params);
var result = jsonResult.rows;

{ "array": [ { "MappingType": "analog", "defaultValue": "0.0", "name": "n1" }, "MappingType": "digital", "defaultValue": "false", "name": "n2" }, ] }

 

Any suggestions?

Thanks

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

 Thanks A_Macierzynsk,

 

I am already working on Deserializer on the client that will take care of that structure.

I still believe that one can write:

var json = {};
json.name = "myName";
json.age = 52;
var result = json;

if Json is the base type of output it will come as:

{
"name": "myName",
"age": 52
}

Why not return an array of objects [{},{},{}..]

 

I prefer to obtain JSON by calling a local service (without doing HTTP request to retrieve JSON).

When this is executed: 

logger.debug(jsonResult.rows);

 I am getting: [object Object],[object Object],..

 

This statement did the trick:

JSON.stringify(jsonResult.rows);

I am getting [{}, {},{}] as the string which I can serialize  to the file repository

with: 

Things["ModelsFileRepository"].SaveJSON(params);

However I again get this:

 {"array":[{},{},..]}

So it seems to be a built-in feature that JSON output is handled this way.

View solution in original post

2 REPLIES 2
A_Macierzynski
14-Alexandrite
(To:rad1)

Hi,

 

After few tests I need to say that it will be really hard to make it like you want.

 

I think that the backend system is adding it automatically, without our knowledge and you cannot do anything with that. ThingWorx is using Rhino engine in services to parse JavaScript code into the Java code which is executed in backend. It might be possible that JSON parsing functions were written like that, to always  add "array" name.

 

In result you need to manage this 'problem' in the client.

 

Best,

Adam

 Thanks A_Macierzynsk,

 

I am already working on Deserializer on the client that will take care of that structure.

I still believe that one can write:

var json = {};
json.name = "myName";
json.age = 52;
var result = json;

if Json is the base type of output it will come as:

{
"name": "myName",
"age": 52
}

Why not return an array of objects [{},{},{}..]

 

I prefer to obtain JSON by calling a local service (without doing HTTP request to retrieve JSON).

When this is executed: 

logger.debug(jsonResult.rows);

 I am getting: [object Object],[object Object],..

 

This statement did the trick:

JSON.stringify(jsonResult.rows);

I am getting [{}, {},{}] as the string which I can serialize  to the file repository

with: 

Things["ModelsFileRepository"].SaveJSON(params);

However I again get this:

 {"array":[{},{},..]}

So it seems to be a built-in feature that JSON output is handled this way.

Top Tags