Skip to main content
Question

Is there a way to order/sort the JSON keys prior to hitting the destination?

  • March 11, 2025
  • 30 replies
  • 93 views

Is there a way to order/sort the JSON keys prior to hitting the destination? I thought prepending the key names with an underscore would bring them on the top but they show up at the bottom of the JSON structure. The ordering looks the way we want it in the pipeline view but come out differently in the actually output.

30 replies

  • Employee
  • March 11, 2025

you can probably do it with regex or other hacks but note that JSON, unlike CSV or some others, is not positional.


  • Author
  • Participating Frequently
  • March 11, 2025

Python has sort_keys param. So nothing like this in JS/cribl?https://www.w3schools.com/python/gloss_python_json_sort.asp


  • Author
  • Participating Frequently
  • March 11, 2025

We have a regex on the destination side to parse the source identifier and that coming towards the end makes our regex expensive.


Jon Rust
Forum|alt.badge.img
  • Employee
  • March 11, 2025

if you're using regex, do you need JSON? Maybe switch you K=V or CSV, where you can specify order.


  • Author
  • Participating Frequently
  • March 11, 2025

Yes we need JSON. Our SIEM parser is only compatible with JSON for this specific source (Azure).


Jon Rust
Forum|alt.badge.img
  • Employee
  • March 11, 2025

but requires regex?


  • Author
  • Participating Frequently
  • March 11, 2025

correct


  • Author
  • Participating Frequently
  • March 11, 2025

SIEM does have a JSON parser but that's available only post processing for property extraction.


Jon Rust
Forum|alt.badge.img
  • Employee
  • March 11, 2025

The Stringify() function in typescript doesn't have a sorting option. Your alternative would be to use the Code function to build your own stringifier where you specify the order


  • Author
  • Participating Frequently
  • March 11, 2025

How do we do that at the event level? Iterate through __e?


Jon Rust
Forum|alt.badge.img
  • Employee
  • March 11, 2025

(Code or Eval)


Jon Rust
Forum|alt.badge.img
  • Employee
  • March 11, 2025

iterating is one option.But since you have a requirement around order, I'd think you'd want to just layout the fields


Jon Rust
Forum|alt.badge.img
  • Employee
  • March 11, 2025

built the textual json by hand, 1 field at a time


Jon Rust
Forum|alt.badge.img
  • Employee
  • March 11, 2025

or, dump all the keys, sort them, then loop through that list to build the string


  • Author
  • Participating Frequently
  • March 11, 2025

Will try different options but would be great if we could have this option as a feature enhancement.


Jon Rust
Forum|alt.badge.img
  • Employee
  • March 11, 2025

```try { var ans = "{" var a = Object.keys(__e.f).sort() for (var k in a) { ans = `${ans}"${a[k]}":"${__e.f[a[k]]}",` } __e.ans = ans.replace(/,$/,'}')}catch (e) {__e.debug = e.message}```


Jon Rust
Forum|alt.badge.img
  • Employee
  • March 11, 2025

of course this may be a problem if your object isn't flat. just a starting point :slightly_smiling_face:


Jon Rust
Forum|alt.badge.img
  • Employee
  • March 11, 2025

where f is your object you want to sort into a stringified JSON


  • Author
  • Participating Frequently
  • March 11, 2025

I'll need to do some testing since the webhook destination dumps the entire event except for the hidden fields. Not sure if it is even possible to sort at the top level.


Jon Rust
Forum|alt.badge.img
  • Employee
  • March 11, 2025

i guess i'm still not clear then on what the goal is :confused:


  • Author
  • Participating Frequently
  • March 11, 2025

If the destination supported syslog, I'll just have to modify the host.


  • Author
  • Participating Frequently
  • March 11, 2025

With webhooks, it gets a bit tricky


Force order (Code function):(put your desired fields in the `first` array, everything else is included with the `...rest`)```let first = ["ProviderId","ProviderName","MachineName"];let [...rest] = Object.keys(e).filter(key => !first.includes(key) && !key.startsWith(""));const ordered = [...first, ...rest];__e['__httpOut'] = JSON.stringify(ordered.reduce((current, val) => Object.assign(current, {[val]: __e[val]}), {}));```


Then change your webhook settings to use the "Custom" format with `__httpOut` as the expression.