Convert JSON to CDT

Hello everyone, I have a process model that is using a custom smart service to perform web service calls. The below json is the response I'm getting. I need to store these results into a database table. Please provide me some guidance on what would be the best approach on parsing this json string into a CDT.

{
  "id":"123",
  "status": "working",
  "reports":[
                 {
                     "name": "report1",
                     "status": "complete",
                     "_links": [
                                 {
                                     "type": "csv",
                                     "href": "http://..../report1.csv"
                                  }
                     ]
                  },
                  {
                     "name": "report2",
                     "status": "errored",
                     "reason": "Something horrible occurred..."
                     "_links": [
                     ]
                  },
                  {
                      "name": "report3",
                       "status": "working",
                       "_links": [
                       ]
                   },
                   {
                       "name": "report4",
                       "status": "pending",
                       "_links": [
                       ]
                    },
  ]
}

Thank you!

  Discussion posts and replies are publicly visible

  • The a!fromJson() function is what you're looking for. It should return you a list of Dictionaries that you can cast to the desired CDT type.

  • Hi mannym,

    As you have two levels of nested CDTs, its better to create Independent CDTs and have each Child CDT have a column referring to its Parent(FK Relationship) and create the respective Tables like below;

    MAIN, REPORT, LINK

    CREATE TABLE LINK (LINK_ID INT(11) NOT NULL AUTO_INCREMENT , LINK_TYPE VARCHAR(255) DEFAULT NULL , HREF VARCHAR(255) DEFAULT NULL , REPORT_ID INT(11) NOT NULL , PRIMARY KEY (LINK_ID));
    
    CREATE TABLE REPORT (REPORT_ID INT(11) NOT NULL AUTO_INCREMENT , NAME VARCHAR(255) DEFAULT NULL , STATUS VARCHAR(255) DEFAULT NULL , REASON VARCHAR(5000) DEFAULT NULL , MAIN_ID INT(11) NOT NULL , PRIMARY KEY (REPORT_ID));
    
    CREATE TABLE MAIN (MAIN_ID INT(11) NOT NULL AUTO_INCREMENT, STATUS VARCHAR(255) DEFAULT NULL);

    If you are using MySQL Database, you can create one Stored Procedure and pass this JSON String as an input parameter to it. Inside the SP, you can parse the JSON and store the values in to the appropriate tables. You can call this SP using Execute Stored Procedure Smart Service from the Appian.

    For parsing JSON in MySQL, please follow the below link;

     

     

    Thank you,

    Arun

  • 0
    Certified Lead Developer
    Hi i believe performing parsing operation using Appian will be quite better, where you can create an Expression rule and by using a!fromJson() and custom manipulation you can return Dictionary or CDT and create a CDT for the Same Structure as your response Dictionary and Cast your Dictionary Data into Array of CDT.

    I believe once you have CDT ready, you can play around it.

    I know using Nested CDT is never a good idea and recommended but as per Appian standard you can go till 1 level of Nested CDT without violating the Appian Standard, and this makes your JOB easy.

    But if you want to go for separate CDT's, then you need to segregate the Dictionary data and then accordingly map them to their respective CDT's which might make your job a bit complex.

    Hope this will give you high level understanding, about which approach fits for your requirement
  • Note 17.4 Integration design can now cast JSON directly to an Appian Data Dictionary
  • Thank you every one for your input, it looks like I'm going to explore the idea of having individual CDTs. Malcolm, we are running 17.2, so that would not be an option for us at this moment.

    Thank you!
    Manny
  • do the following:
    local!fromArray: a!fromJson({
    "id":"123",
    "status": "working",
    "reports":[
    {
    "name": "report1",
    "status": "complete",
    "_links": [
    {
    "type": "csv",
    "href": "http://..../report1.csv"
    }
    ]
    },
    {
    "name": "report2",
    "status": "errored",
    "reason": "Something horrible occurred..."
    "_links": [
    ]
    },
    {
    "name": "report3",
    "status": "working",
    "_links": [
    ]
    },
    {
    "name": "report4",
    "status": "pending",
    "_links": [
    ]
    },
    ]
    }),

    local!cdtData: a!forEach(items: local!fromArray, expression:{'type!{namespace}CDT(col1: fv!items[fv!index].col1, col2:fv!items[fv!index].col2, col3:fv!items[fv!index].col3, col4:fv!items[fv!index].col4,...etc),)