Encryption: Hash Functions in JSONata ($md5 and $sha256)

JSONata provides cryptographic hash functions that allow values to be transformed into unique and deterministic representations.

These functions can be used to:

  • Ensure data integrity

  • Create unique identifiers

  • Anonymize sensitive values

  • Ensure compatibility with external systems

Supported functions:

  • $md5(value)

  • $sha256(value, digestFormat?)

Difference Between MD5 and SHA-256

Algorithm
Hash Size
Security
Common Usage

MD5

32 characters (128 bits)

Weak, collision-prone

Checksums, simple validation

SHA-256

64 characters (256 bits)

Strong, recommended

Authentication, blockchain, cryptography

Function $md5(value)

  • Calculates the MD5 hash of the provided value.

  • Return: fixed 32-character hexadecimal string

  • Digest parameter: not supported

Example

Input:
{ "status": "200" }

Expression:
$md5(status)

Output:
{ "data": "3644a684f98ea8fe223c713b77189a77" }

Function $sha256(value, digestFormat?)

  • Calculates the SHA-256 hash of the provided value.

  • Return: fixed 64-character hexadecimal string (by default)

  • Digest parameter: supports digestFormat to change the output format

Supported formats (digestFormat)

Value
Description
Example Output

"hex" (default)

Hexadecimal representation

27badc983df1780b60c2...

"base64"

Standard Base64

J7cmD3xeAtgwrP6nToZoA...

"base64url"

Base64 adapted for URLs/JWTs

J7cmD3xeAtgwrP6nToZoA...

Practical Examples

SHA-256 in hex (default)

Input:
{ "status": "200" }

Expression:
$sha256(status)


Output:
{ "data": "27badc983df1780b60c2b3fa9d3a19a00e46aac798451f0febdca52920faaddf" }

SHA-256 in Base64

Input:
{ "status": "200" }

Expression:
$sha256(status,"base64")

Output:
{ "data": "J7cmD3xeAtgwrP6nToZoA5GqseYRR8P69y1KSD6rd8=" }

SHA-256 in complex JSON (hex)

Input:
{ "response": "{...JSON object...}" }

Expression:
$sha256(response)

Output:
{ "data": "885826bd782b9b92cca593385729cc17ea05ca3f8f68935c5a1613102dfc066" }

SHA-256 in complex JSON (Base64)

Input:
{ "response": "{...JSON object...}" }

Expression:
$sha256(response,"base64")

Output:
{ "data": "CFAmvXgrCbksyIkZhXApzBfqBco/j2A5XFoWExAt/AY=" }

Resumo dos tipos de digest

Digest Format
Description

hex

Hexadecimal representation (default)

base64

Standard Base64 representation

base64url

Base64 adapted for URLs/JWTs

latin1

Each byte converted into a Latin-1 character

utf8

Return handled as a UTF-8 string

binary/buffer

Return as raw binary data (when supported)

Last updated