'How to stringify JSON using JQ
Using JQ I would like to take a complex JSON object that includes JSON embedded as strings and then turn it all into a valid string I can easily embed in other JSON objects.
For example, lets say I have this json object:
{
"region": "CA",
"waf_rule_tags": "{\"RULEID:942100\":[\"application-multi\",\"language-multi\",\"platform-multi\",\"attack-sqli\",\"OWASP_CRS/WEB_ATTACK/SQL_INJECTION\",\"WASCTC/WASC-19\",\"OWASP_TOP_10/A1\",\"OWASP_AppSensor/CIE1\",\"PCI/6.5.2\"]}"
}
I need to turn this all into the following string:
"{\"region\": \"CA\",\"waf_rule_tags\": \"{\\\"RULEID:942100\\\":[\\\"application-multi\\\",\\\"language-multi\\\",\\\"platform-multi\\\",\\\"attack-sqli\\\",\\\"OWASP_CRS/WEB_ATTACK/SQL_INJECTION\\\",\\\"WASCTC/WASC-19\\\",\\\"OWASP_TOP_10/A1\\\",\\\"OWASP_AppSensor/CIE1\\\",\\\"PCI/6.5.2\\\"]}\"}"
That way I can take this string and insert it exactly under the text field of another JSON object to create the following.
{
"title": "12345-accesslogs",
"text": "{\"region\": \"CA\",\"waf_rule_tags\": \"{\\\"RULEID:942100\\\":[\\\"application-multi\\\",\\\"language-multi\\\",\\\"platform-multi\\\",\\\"attack-sqli\\\",\\\"OWASP_CRS/WEB_ATTACK/SQL_INJECTION\\\",\\\"WASCTC/WASC-19\\\",\\\"OWASP_TOP_10/A1\\\",\\\"OWASP_AppSensor/CIE1\\\",\\\"PCI/6.5.2\\\"]}\"}",
"priority": "normal",
"tags": ["environment:test"],
"alert_type": "info"
}
Solution 1:[1]
I found @peak's comment in their own answer so useful I wanted to make it an answer on its own:
$ echo '{ "foo": [ "bar", "baz" ] }' | jq tostring
"{\"foo\":[\"bar\",\"baz\"]}"
Solution 2:[2]
JQ provides tojson and fromjson filters for that.
Note about the answers proposing tostring: The JQ manual says about tojson:
The
tojsonbuiltin differs fromtostringin thattostringreturns strings unmodified, whiletojsonencodes strings as JSON strings.
So I think tojson is always correct but tostring is only correct as long as you only use complex objects.
Solution 3:[3]
A variation on this is to stringify a given key within a JSON structure using tostring something like this:
echo '{"a":1,"b":{"c":{"d":2},"e":3}}' | jq '.b.c=(.b.c|tostring)'
...which targets path b.c to give:
{
"a": 1,
"b": {
"c": "{\"d\":2}",
"e": 3
}
}
(As per A.H.'s answer, tojson might be useful instead of tostring if you want strings to be json-string-encoded as well)
Solution 4:[4]
DeviceDisplay is there - see maui essentials source.
The namespace is Microsoft.Maui.Essentials.
That's where to look, for anything that was in Xamarin.Essentials.
"Essentials" are in Extensions Nuget. Make sure your project has Nuget "Microsoft.Maui.Extensions" installed. (Should automatically be installed when create a Maui project.)
And at top of a source file, add using Microsoft.Maui.Essentials;.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | A.H. |
| Solution 3 | |
| Solution 4 |
