How to create Json Parameter based on CDT of Array?

Certified Associate Developer

Hello All,

I'm having some difficulty for creating Json parameter based on my cdt. Here is the scenario,

 

Suppose my CDT is as below

[ID:12,Identifier:"Abc",Label:"NumberOfPages",Value:"30"],

[ID:13,Identifier:"Pqr",Label:"LastModified",Value:"2018-12-11"]

 

Now I need to create a Json parameter in such a way that it consists of label and value as a actual parameter and its value i.e. Json output should be something like

{"NumberOfPages":"30",LastModified":"2018-12-11"}

 

Any suggestion here would be appreciated. Thanks in advance

  Discussion posts and replies are publicly visible

Parents Reply
  • 0
    Certified Associate Developer
    in reply to Mike Schmitt
    Ok, Let me explain what I need exactly. Consider below is my CDT

    local!CDT:{
    ID:12,
    Identifier:"Abc",
    Label:"NumberOfPages" ,
    Value:30
    }


    Now in the above CDT, We have total 4 fields i.e ID,Identifier,Label and Value. Now I need to convert the combination of Label and Value into Json format. i.e. My Value inside Label field is "NumberOfPages" and inside Value is "30".So I want to merge this two values in the form of Json like

    {"NumberOfPages":"30"}
Children
  • 0
    Certified Lead Developer
    in reply to viveku3486

    For this you would need some sort of custom code, as this isn't really a use case a!toJson is intended to work with.

    The only suggestion I can think of is, use your original CDT and formulate a custom dictionary and then convert that to JSON, and afterwards manually swap out values within the resulting JSON string.  Note that this is a little complicated and might not scale very well.

    load(
      local!myCdt: {
        ID: 12,
        Identifier: "Abc",
        Label: "NumberOfPages",
        Value: 30,
        LastModified: today()
      },
      
      local!customDictionary: {
        replaceWithLabel: local!myCdt.Value
      },
      
      local!initialJson: a!toJson(local!customDictionary),
      
      substitute(
        local!initialJson,
        "replaceWithLabel",
        local!myCdt.Label
      )
    )

    Result:

    {"NumberOfPages":30}
  • 0
    Certified Associate Developer
    in reply to Mike Schmitt
    Hey Mike,

    Thanks for your quick responses and providing the help specific to what was needed. I really appreciate your efforts and time. You are CHAMP !!!
  • 0
    A Score Level 2
    in reply to viveku3486
    If you want to user an array of CDT, the you need to user forEach to loop through the each record, else your json will be messed up.
  • 0
    Certified Lead Developer
    in reply to chandu

    Fair point - presuming the original input is a CDT array with different Labels, we would want to handle the conversion in a loop.

    with(
      local!myCdt: {
        {
          ID: 12,
          Identifier: "Abc",
          Label: "NumberOfPages",
          Value: 30
        },
        {
          ID: 47,
          Identifier: "XYZ",
          Label: "LastModified",
          Value: today()
        }
      },
      
      local!customDictionary: a!toJson(
        a!forEach(
          local!myCdt,
          with(
            local!initialJson: a!toJson(
              {
                replaceWithLabel: fv!item.Value
              }
            ),
            
            local!convertedJson: substitute(
              local!initialJson,
              "replaceWithLabel",
              fv!item.Label
            ),
            
            a!fromJson(local!convertedJson)
          )
        )
      ),
      
      local!customDictionary
    )

    Output: 

    [{"NumberOfPages":30},{"LastModified":"2018-06-29Z"}]