Regular Expression
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2023 03:27 AM
Hello Experts,
I have the below payload which I am passing using the POSTMAN tool. when I passed payload through Postman my nested JSON data: colon is replacing with = equal to.
i did not find any error in my JSON so I have written before business rule by taking the help of chat GPT to resolve equal to issue .i have pasted code below.my code is working 90% but for node key when i pass value like *ANNIE-IBM,
.NET Framework,/dev/dm-0 its giving me output like {"Node":*"ANNIE-IBM"}its excluding * same for .net and /.
i have pasted code below .please guide me .
thank you
POSTMAN JSON DATA
(function executeRule(current, previous /*, ... other inputs */ ) {
// Get the JSON-like string field from the record (assuming the field is named "jsonField")
var jsonLikeString = current.u_ma_fld_additional_information;
// Replace all '=' with ':' and wrap keys and values with double quotes
var validJsonData = jsonLikeString.replace(/([A-Za-z0-9*_-]+)\s*=/g, '"$1":').replace(/=(?![>"])/g, '":"').replace(/(?!:")(\b[A-Za-z0-9*_-]+\b)(?=[,}])/g, '"$1"');
// Function to handle nested objects
function fixNestedObjects(match, p1, offset, string) {
// If the matched group contains '{', it means it's the start of a nested object
if (match.includes('{')) {
return match;
}
// Replace commas with ', ' to separate the key-value pairs correctly
return match.replace(/,/g, ', ');
}
// Handle nested objects recursively
var nestedRegex = /{[^{}]*}/g;
validJsonData = validJsonData.replace(nestedRegex, fixNestedObjects);
// Set the modified JSON string back to the field
current.u_ma_fld_additional_information = validJsonData;
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2023 05:04 AM
@Ankur Bawiskar @Ravi Chandra_K please guide me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2023 06:15 AM
Hi @Mark Wood
This should help -
(function executeRule(current, previous /*, ... other inputs */ ) {
// Get the JSON-like string field from the record (assuming the field is named "jsonField")
var jsonLikeString = current.u_ma_fld_additional_information;
try {
// Parse the JSON-like string to a JavaScript object
var jsonObject = JSON.parse(jsonLikeString);
// Convert the JavaScript object back to a valid JSON string
var validJsonData = JSON.stringify(jsonObject, null, 2);
// Set the modified JSON string back to the field
current.u_ma_fld_additional_information = validJsonData;
} catch (e) {
gs.error('Error parsing JSON: ' + e.message);
}
})(current, previous);
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Regards,
Tushar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2023 10:33 PM
Hello @Tushar I tried on my PDI getting output like {Node=*ASSET-IBM, Key=Value, demo={test=data, test2={key=value, demo7={key=value}}}}which not correct by using my own code i was getting output like {"Node":*"ASSET-IBM", "Key":"Value", "demo":{"test":"data", "test2":{"key":"value", "demo7":{"key":"value"}}}}
which is mostly correct but that value is not coming correctly .it should be "*ASSET-IBM" same issue I am facing for other ci like