1214
Views
19
Comments
Convert json to structure
Question

Hi

I want to convert this json to strcuture and problems with this is I'm getting list of elements and values on each row for nodes.

"[{\"UnplannedDown\":[{\"reasons\":[{\"count\":\"3.04\",\"description\":\"No reason provided\",\"reasonCode\":\"NA\"},{\"count\":\"4.32\",\"description\":\"RC_LACKOIL\",\"reasonCode\":\"RC_LACKOIL\"}],\"percentage\":[{\"count\":\"7.36\"}]}]},

{\"PlannedDown\":[{\"reasons\":[{\"count\":\"0.76\",\"description\":\"RC_LACKOIL\",\"reasonCode\":\"RC_LACKOIL\"},{\"count\":\"0.76\",\"description\":\"No reason provided\",\"reasonCode\":\"NA\"}],\"percentage\":[{\"count\":\"1.52\"}]}]},{\"Running\":[{\"reasons\":[{\"count\":\"87.32\",\"description\":\"No reason provided\",\"reasonCode\":\"NA\"}],\"percentage\":[{\"count\":\"87.32\"}]}]},

{\"UnplannedIdling\":[{\"reasons\":[{\"count\":\"0\",\"description\":\"RC_LACKOIL\",\"reasonCode\":\"RC_LACKOIL\"}],\"percentage\":[{\"count\":\"0\"}]}]},

{\"PlannedIdling\":[{\"reasons\":[{\"count\":\"3.79\",\"description\":\"RC_LACKOIL\",\"reasonCode\":\"RC_LACKOIL\"},{\"count\":\"0.01\",\"description\":\"No reason provided\",\"reasonCode\":\"NA\"}],\"percentage\":[{\"count\":\"3.8\"}]}]}]"

What is the best way I can convert this to struture?

Also I need it from json deserilizer

2023-03-16 16-29-51
Paulo Rosário

Hello Suraj,

If you have the JSON file defined Outsystems allows you to directly convert it to a structure 

Just copy/paste the text and you should be fine. 

Make sure that the JSON is valid of course. You can use this tool to do that. 

Hope it helps!

Paulo Rosário

 


2018-11-06 14-26-44
Suraj Borade

Hi Paulo

Thanks for reply.

Actually, I already have done that but when I pass that structure, I'm not getting any values.



Not sure why it is returning empty.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Suraj,

I just created a test app (see attachment), and the data you supplied seems to work fine:

So your problem must lie elsewhere.


TestJsonTradWeb.oml
2018-11-06 14-26-44
Suraj Borade

Hi sir

Thanks for taking time to develop.

But why am I getting output like this?


2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

That's a good question. Did you compare my test app with your code? Any differences? Or are you receiving the JSON data from somewhere, and is the structure different (as you can see I use hardcoded values)?

2018-11-06 14-26-44
Suraj Borade


This output is from the test app you have provided which I installed on my PE. I'm receiving JSON data from rest api. There's no difference between your code and my code.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

In that case, check the JSON that is sent via the API (by adjusting the logging levels of the REST API in Service Center), and manually check whether there's a difference in thhe JSON with the JSON you based the structure on.

2018-11-06 14-26-44
Suraj Borade

My whole rest api output is this.

[{"cursor":0,"apiName":"event_g.machineAvailabilityInsights","pageSize":0,"resultSet":[{"thingId":283,"thingName":"ABB Robot Tender","insights":"[{\"UnplannedDown\":[{\"reasons\":[{\"count\":\"3.04\",\"description\":\"No reason provided\",\"reasonCode\":\"NA\",\"category\":\"UnplannedDown\"},{\"count\":\"4.32\",\"description\":\"RC_LACKOIL\",\"reasonCode\":\"RC_LACKOIL\",\"category\":\"UnplannedDown\"}],\"percentage\":[{\"count\":\"7.36\"}]}]},{\"PlannedDown\":[{\"reasons\":[{\"count\":\"0.76\",\"description\":\"RC_LACKOIL\",\"reasonCode\":\"RC_LACKOIL\",\"category\":\"PlannedDown\"},{\"count\":\"0.76\",\"description\":\"No reason provided\",\"reasonCode\":\"NA\",\"category\":\"PlannedDown\"}],\"percentage\":[{\"count\":\"1.52\"}]}]},{\"Running\":[{\"reasons\":[{\"count\":\"87.32\",\"description\":\"No reason provided\",\"reasonCode\":\"NA\",\"category\":\"Running\"}],\"percentage\":[{\"count\":\"87.32\"}]}]},{\"UnplannedIdling\":[{\"reasons\":[{\"count\":\"0\",\"description\":\"RC_LACKOIL\",\"reasonCode\":\"RC_LACKOIL\",\"category\":\"UnplannedIdling\"}],\"percentage\":[{\"count\":\"0\"}]}]},{\"PlannedIdling\":[{\"reasons\":[{\"count\":\"3.79\",\"description\":\"RC_LACKOIL\",\"reasonCode\":\"RC_LACKOIL\",\"category\":\"PlannedIdling\"},{\"count\":\"0.01\",\"description\":\"No reason provided\",\"reasonCode\":\"NA\",\"category\":\"PlannedIdling\"}],\"percentage\":[{\"count\":\"3.8\"}]}]}]"}]}]


Out of this, I want output in structure format for insights node.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Yeah, so need a structure that encompasses all of this, right? So create that structure (or use the JSON to Structure), and it should work right? I mean, it's not going to work with the structure from my test app since it's missing a level or two, but just add that and it should work.

2022-09-20 06-28-12
Nitish Kumar

Hi @Suraj Borade,

I tried to check your JSON and test it with Outsystems as you can see from the below screenshot, it's working fine

You just have to select the Data Type of JSONDeserialize as a List of your Structure because your JSON stats with [] which denotes an array here and can only be held by a list of structure.

I hope this solves your issue.

Thanks & Regards

Nitish Kumar


2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Though this is true, this can't be the problem he's having: if you use the structure, instead of a list of structures, as datatype, you get a runtime error, not just an empty list (ask me how I know 😂).

2018-11-06 14-26-44
Suraj Borade

Hi Nitish,

Can you please paste your oml here?

2022-09-20 06-28-12
Nitish Kumar

Hi Suraj,

Please find the OML attached.

Thanks & Regards

TestJSON_Module.oml
2022-09-20 06-28-12
Nitish Kumar

@Kilian Hekhuis, I also tried not taking this structure as a list and it didn't give me any error, but only give me the empty structure as it was happing with Suraj.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Mmm, really? Did you try in a Reactive or Traditional Web? I used TW, but maybe Reactive behaves diferently?

2022-09-20 06-28-12
Nitish Kumar

@Kilian Hekhuis it was Reactive.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Thanks, good to know it works differently there.

2018-11-06 14-26-44
Suraj Borade

Hi guys,

Thanks for taking time.

I should have made it clear that I'm working in Reactive Web Applications.

I am using list as done by @Nitish Kumar 

But my question is that I'm getting 5 different list elements in the output as shown here.






My question is can't I have all values at [0] index itself?

Thanks

2022-09-20 06-28-12
Nitish Kumar

Hi @Suraj Borade ,

Yes, you can access the [0] of the structure if you want.

Sending you the updated Module with it.

TestJSON_Module.oml
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.