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

Groovy Script to extract JSON elements for Content modifier

0 Likes
6,926

I have a groovy script below to parse an incoming JSON message to extract values and store as properties.

I want to use these property values in the content modifier for a JSON payload to an external system.

The output properties have the following format:

But I need only the values eg. Hyderabad without the square brackets.

How do I remove the brackets in the output message in the Groovy script?

Or can I do this in the Content Modifier?

Thank you.

Best Regards,

Lorna

Accepted Solutions (1)

Accepted Solutions (1)

vadimklimov
Active Contributor

Hello Lorna,

May I take a step back and firstly draw your attention to why those square brackets appear - after that is settled, a solution to how to remove them shall become clearer.

An input JSON document that you provided, is an array of objects: the document contains opening and closing square brackets that indicate an array, and inside them, there is an object with properties BillingCity, BPRecordID__c, BillingAddress (a nested object), AccountGrp__c and CreatedDate (the object is indicated by curly brackets). Generally speaking, an array can contain no elements, a single element or several elements - in your case, the array only contains a single object.

When you use JsonSlurper and parse a JSON document with it, it differentiates arrays, objects and their attributes correspondingly, and creates appropriate Groovy data structures to represent the parsed document structure and hold data from it. For example, JSON arrays get translated to Groovy lists.

When you apply certain GPath expressions to access structured data in Groovy (for example, in this case, that is a representation of a parsed JSON document), under the hood, it accesses corresponding Groovy objects that were created during parsing.

As an example, in your script, a call object.BillingCity attempts to retrieve an attribute BillingCity of JSON objects that are elements of an array (since object is a list representation of a JSON array). Even though a result of this query applied to your input document is just a single value, this value is a part of a list (think of it as a list that contains just a single element).

Next, an attempt to stringify a list in Groovy would usually result in comma-and-space delimited stringified values of elements contained in the list, surrounded by square brackets. This is why you get square brackets in values that are placed in properties and that are further accessed in a Content Modifier step. For example, you get [Hyderabad].

Now, how can we remove those square brackets from a value assigned to a property. If you are sure that you don't need the entire content of the list, but only a specific element's value from it, you can retrieve that particular element's value. And if you are very sure that the list only contains a single element in it (an input document contains only one object in the array, like in your example), or if you only want to get the first value from the list, then, building on top of an example with object.BillingCity, you can retrieve the first (and as per your input example, the only) value by modifying a call to either of the below:

  • object[0].BillingCity - this will retrieve the first element in the list and access its property BillingCity,
  • object.BillingCity[0] or object.BillingCity.first() - this will retrieve a list of BillingCity values and then will take the first of them.

Either of the above options will result in Hyderabad, rather than [Hyderabad]. Though, due to the way and sequence how objects are accessed, I personally favour the first option as it makes an intent (firstly access the first element in the list, then retrieve a value of its property BillingCity) clearer.

If an input JSON document always contains only a single object, then it might be worth considering if usage of an array can be omitted (thus, resulting in removal of outer square brackets in the input JSON document), as it is redundant. If that change to the input JSON document is applied, your way of accessing the needed attributes will work as you want.

A word of caution here is: please check that you will not come across a scenario where an input JSON document contains more than one object in the array, as if that is also possible, the above way of accessing only the first object in the array, querying its properties and ignoring the rest of objects that are found in the JSON document, might be inaccurate and might need to be reconsidered.

Regards,

Vadim

0 Likes

Hello Vadim,

Thank you very much for taking the time to explain this so clearly and provide suggestions for what to do and not to do.

I have implemented the change and it works now.

Once again, thank you.

Best Regards,

Lorna

Answers (3)

Answers (3)

Hello Sriprasad,

Here is my JSON message input:

Here is the groovy script:

Property Element Output Values:

I use the Properties in the Content Modifier body in a JSON format like this:

I need the JSON output with the values without the square brackets.

How and where can I remove the square brackets?

Thank you.

Best Regards,

Lorna

kc_kristris
Active Participant
0 Likes

Hello Lorna,

Before setting up properties remove the square brackets.

text = text.replaceAll("[]", "");

Regards,

Kris

0 Likes

Hi Kris,

Thanks for your reply.

I need to remove the brackets after the properties to map the JSON input to a JSON output.

The JSON output will be used as input to call another API service.

After the groovy script, I used a content modifier.

In the body I added in the JSON output with the $(property.BillingCity} but it brings in the square brackets as below:

I want to remove the square brackets from the property values.

Is this possible?

Thanks.

Best Regards,

Lorna

Sriprasadsbhat
Active Contributor
0 Likes

Hello Lorna,

I think your input data is missing.Please do share input data and expected output including details of element for which data comes from property.

Regards,

Sriprasad Shivaram Bhat