Skip to content

Commit

Permalink
Initial Code to Extract JSON data from WebSocket message
Browse files Browse the repository at this point in the history
  • Loading branch information
ncoblentz committed Jan 18, 2024
1 parent d8595ee commit 32e8cc4
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Proxy/Websocket/ExtractPayloadToNotes.bambda
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Extracts JSON elements from the WebSocket message and displays it in the "Notes" column of the WebSocket History tab
*
* @author Nick Coblentz (https://github.com/ncoblentz)
*
**/

//The bambda will search for json elements with the following keys. The keys below are just examples. Add the keys you want to include here:
List<String> terms = List.of("target","error");

if (!message.annotations().hasNotes()) {
StringBuilder builder = new StringBuilder();
String payload = utilities().byteUtils().convertToString(message.payload().getBytes());
terms.forEach(term -> {
Matcher m = Pattern.compile("\"" + term + "\":\"([^\"]+)\"", Pattern.CASE_INSENSITIVE).matcher(payload);
while (m.find() && m.groupCount() > 0) {
for (int i = 1; i <= m.groupCount(); i++) {
if (m.group(i) != null)
builder.append(term + ": " + m.group(i) + " ");
}
}
});
message.annotations().setNotes(builder.toString());
}
return true;

0 comments on commit 32e8cc4

Please sign in to comment.