Question

How to add static string to array objects based on condition in JSON response

  • 6 September 2021
  • 3 replies
  • 19 views

I have created an FME workspace that return some validation messages in JSON. The output looks something like this:

{

  "Response" : {

   "transaction" : {

     "id" : "78234626376472399",

     "transactionDetail" : {

      "validationResult" : [

        {

         "testCode" : "1001",

         "testResult" : "PASSED",

        },

        {

         "testCode" : "1002",

         "testResult" : "FAILED",

 "failMessage" : "fail Message 1"

        },

        {

         "testCode" : "1003",

         "testResult" : "PASSED",

        },

        {

         "testCode" : "1004",

         "testResult" : "FAILED",

 "failMessage" : "fail Message 2"

        },

  {

         "testCode" : "1005",

         "testResult" : "PASSED",

        },

  {

         "testCode" : "1006",

"testResult" : "FAILED",

 "failMessage" : "fail Message 3"

        }

  {

         "testCode" : "1007",

         "testResult" : "PASSED",

        }

      ]

     }

   }

  }

}

 

Once I have generated this message, I need to add static string before the first failMessage and after last failMessage. This will be used by the application to display a message like:

You cannot proceed to next stage because: (This is static text)

  • FailMessage 1
  • FailMessage 2
  • FailMessage 3

Please contact abc for detail. (This is static text)

How can I achieve this. I am using Sql Executor to pull data from DB and then JSON templater to create the JSON message. How to do I achieve appending these strings before the first fail message and after last fail messages? As you can see, the validationResult array may have both passed and failed results.

Please help.


3 replies

Badge +6

Hi @sseth2011​, You could achieve this using the AttributeManager. Assuming you are writing your JSON out into a text file, you can set the attribute value for the text_line_data attribute in the AttributeManager. Simply type out your static text and select your formatted JSON message under the FME Feature Attributes within the Text Editor.

Hi @sseth2011​, You could achieve this using the AttributeManager. Assuming you are writing your JSON out into a text file, you can set the attribute value for the text_line_data attribute in the AttributeManager. Simply type out your static text and select your formatted JSON message under the FME Feature Attributes within the Text Editor.

Thank you for your reply. Could you please provide some guideline on how to achieve this.

How do I figure out from the array the first and last fail messages and then add more strings to failMessage for that element?

Badge +6

Thank you for your reply. Could you please provide some guideline on how to achieve this.

How do I figure out from the array the first and last fail messages and then add more strings to failMessage for that element?

I added the AttributeManager to one of our tutorial on Writing JSON with the JSONTemplater.

Hopefully I understood your question correctly, let me know if this helps.

Reply