https://github.com/devlooped/jq
A nuget distribution of the official JQ implementation, for easy consumption from .NET
https://github.com/devlooped/jq
Last synced: about 1 year ago
JSON representation
A nuget distribution of the official JQ implementation, for easy consumption from .NET
- Host: GitHub
- URL: https://github.com/devlooped/jq
- Owner: devlooped
- License: mit
- Created: 2024-03-20T12:02:28.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-29T00:03:05.000Z (about 1 year ago)
- Last Synced: 2025-04-29T01:25:19.300Z (about 1 year ago)
- Language: C#
- Size: 85 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
- License: license.txt
Awesome Lists containing this project
README
# ./jq for .net
[](https://www.nuget.org/packages/Devlooped.JQ)
[](https://www.nuget.org/packages/Devlooped.JQ)
[](https://github.com/devlooped/jq/blob/main/license.txt)
[](https://github.com/devlooped/jq/actions)
Packs the [jq](https://jqlang.github.io/jq/) binaries for easy execution
from dotnet applications running on Linux (AMD64 and ARM64), macOS (AMD64 and ARM64)
and Windows (AMD64 and i386).
When JsonPath falls short, `jq` is the obvious next step in flexibility
and power for JSON manipulation.
> jq is like sed for JSON data - you can use it to slice and filter and map
> and transform structured data with the same ease that sed, awk, grep and
> friends let you play with text.
Learn more about `jq` at [https://jqlang.github.io/jq/](https://jqlang.github.io/jq/).
## Usage
```csharp
var name = await JQ.ExecuteAsync(
"""
{
"name": "John",
"age": 30
}
""",
".name"));
```
The `JQ.Path` static property provides the full path to the jq binary that's appropriate
for the current OS and architecture so you can execute it directly if needed.
## Examples
The following is a real-world scenario where [WhatsApp Cloud API messages](https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples)
are converted into clean polymorphic JSON for nice OO deserialization via System.Text.Json.
Rather than navigating deep into the JSON structure, we can use `jq` to transform the payload
into what we expect for deserialization of a text message:
```json
{
"id": "wamid.HBgNMTIwM==",
"timestamp": 1678902345,
"to": {
"id": "792401583610927",
"number": "12025550123"
},
"from": {
"name": "Mlx",
"number": "12029874563"
},
"content": {
"$type": "text",
"text": "😊"
}
}
```
The original JSON looks like the following:
```json
{
"object": "whatsapp_business_account",
"entry": [
{
"id": "813920475102346",
"changes": [
{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "12025550123",
"phone_number_id": "792401583610927"
},
"contacts": [
{
"profile": { "name": "Mlx" },
"wa_id": "12029874563"
}
],
"messages": [
{
"from": "12029874563",
"id": "wamid.HBgNMTIwM==",
"timestamp": "1678902345",
"text": { "body": "\ud83d\ude0a" },
"type": "text"
}
]
},
"field": "messages"
}
]
}
]
}
```
The following JQ query turns the latter info the former:
```jq
.entry[].changes[].value.metadata as $phone |
.entry[].changes[].value.contacts[] as $user |
.entry[].changes[].value.messages[] |
{
id: .id,
timestamp: .timestamp | tonumber,
to: {
id: $phone.phone_number_id,
number: $phone.display_phone_number
},
from: {
name: $user.profile.name,
number: $user.wa_id
},
content: {
"$type": "text",
text: .text.body
}
}
```
This allows you to focus your C# code into the actual object model you want
to work with, rather than the one imposed by the JSON format of external APIs.
See this code in action at [Devlooped.WhatsApp](https://github.com/devlooped/WhatsApp/blob/main/src/WhatsApp/Message.cs).
# Sponsors
[](https://github.com/clarius)
[](https://github.com/MFB-Technologies-Inc)
[](https://github.com/torutek-gh)
[](https://github.com/drivenet)
[](https://github.com/Keflon)
[](https://github.com/tbolon)
[](https://github.com/kfrancis)
[](https://github.com/twenzel)
[](https://github.com/unoplatform)
[](https://github.com/dansiegel)
[](https://github.com/rbnswartz)
[](https://github.com/jfoshee)
[](https://github.com/Mrxx99)
[](https://github.com/eajhnsn1)
[](https://github.com/IxTechnologies)
[](https://github.com/davidjenni)
[](https://github.com/Jonathan-Hickey)
[](https://github.com/akunzai)
[](https://github.com/jakobt)
[](https://github.com/KenBonny)
[](https://github.com/SimonCropp)
[](https://github.com/agileworks-eu)
[](https://github.com/sorahex)
[](https://github.com/arsdragonfly)
[](https://github.com/vezel-dev)
[](https://github.com/ChilliCream)
[](https://github.com/4OTC)
[](https://github.com/v-limo)
[](https://github.com/jordansjones)
[](https://github.com/DominicSchell)
[](https://github.com/sponsors/devlooped)
Â
[Learn more about GitHub Sponsors](https://github.com/sponsors)