[exim] Re: a couple more problems with parsing JSON

Top Page
Delete this message
Reply to this message
Author: Victor Ustugov
Date:  
To: Jeremy Harris via Exim-users
Subject: [exim] Re: a couple more problems with parsing JSON
Jeremy Harris via Exim-users wrote on 24.05.2023 00:52:
> On 21/05/2023 19:25, Victor Ustugov via Exim-users wrote:
>> without comma:
>> # exim -be '${extract jsons{opt2}{ \{ "opt1":"1","opt2":"2 3" \}}}'
>> 2 3
>>
>> with comma
>> # exim -be '${extract jsons{opt2}{ \{ "opt1":"1","opt2":"2,3" \}}}'
>> Failed: missing '"' wrapping string result for extract jsons
>
> Please open a bug for this.  Something like
> "comma in json data not handled by jsons extract".


Done:

https://bugs.exim.org/show_bug.cgi?id=3006


>> # exim -be '${extract json{opt2}{ \{ "opt1":"1","opt2":"2 \"3\"" \}}}'
>
> That isn't a valid JSON input string; the backslash is interpreted
> by Exim and does not become part of the string.


Ok. Let me show another example.


In a previous post, I wrote about a request to https://ipinfo.io.
This is real case.

# exim -be '${sg{${readsocket{inet:ipinfo.io:443}{GET /91.202.74.134
HTTP/1.1\r\nHost: ipinfo.io\r\nConnection:
close\r\n\r\n}{20s:tls=yes:sni=ipinfo.io}{\n}{socket
failure}}}{\N^((.+)\r?\n)+\r?\n\N}{}}'
{
"ip": "91.202.74.134",
"hostname": "mx.org.ua",
"city": "Kyiv",
"region": "Kyiv City",
"country": "UA",
"loc": "50.4547,30.5238",
"org": "AS44600 \"GIGATRANS UKRAINE\", LLC",
"postal": "03027",
"timezone": "Europe/Kyiv",
"readme": "https://ipinfo.io/missingauth"
}


There is a comma in the "org" value. We need to remove it due to the
above error.

# exim -be '${sg{${sg{${readsocket{inet:ipinfo.io:443}{GET
/91.202.74.134 HTTP/1.1\r\nHost: ipinfo.io\r\nConnection:
close\r\n\r\n}{20s:tls=yes:sni=ipinfo.io}{\n}{socket
failure}}}{\N^((.+)\r?\n)+\r?\n\N}{}}}{\N, LLC\N}{ LLC}}'
{
"ip": "91.202.74.134",
"hostname": "mx.org.ua",
"city": "Kyiv",
"region": "Kyiv City",
"country": "UA",
"loc": "50.4547,30.5238",
"org": "AS44600 \"GIGATRANS UKRAINE\" LLC",
"postal": "03027",
"timezone": "Europe/Kyiv",
"readme": "https://ipinfo.io/missingauth"
}


And now I will try to extract "org" value:

# exim -be '${extract
json{org}{${sg{${sg{${readsocket{inet:ipinfo.io:443}{GET /91.202.74.134
HTTP/1.1\r\nHost: ipinfo.io\r\nConnection:
close\r\n\r\n}{20s:tls=yes:sni=ipinfo.io}{\n}{socket
failure}}}{\N^((.+)\r?\n)+\r?\n\N}{}}}{\N, LLC\N}{ LLC}}}}'
Failed: missing '"' wrapping string key for extract json


There is no problem with extracting previous text values:

# exim -be '${extract
json{region}{${sg{${sg{${readsocket{inet:ipinfo.io:443}{GET
/91.202.74.134 HTTP/1.1\r\nHost: ipinfo.io\r\nConnection:
close\r\n\r\n}{20s:tls=yes:sni=ipinfo.io}{\n}{socket
failure}}}{\N^((.+)\r?\n)+\r?\n\N}{}}}{\N, LLC\N}{ LLC}}}}'
"Kyiv City"


There is no any problem with double quotes in "static" JSON:

# exim -be '${extract json{key2}{ \{ "key1":"value 1","key2":"text
\\"value\\" 2" \} }}'
"text \"value\" 2"

# exim -be '${extract jsons{key2}{ \{ "key1":"value 1","key2":"text
\\"value\\" 2" \} }}'
text \"value\" 2


There is one remark regarding the last example. I think that extract
json need to remove the escape of double quotes.
I was expecting `text "value" 2`, not `text \"value\" 2`

Although there are no problems with the "static" JSON examples shown
above, in real life I need to process JSON received from the outside
(rspamd, responses to API requests, and so on).


At the moment I have implemented the extract json and extract jsons in
embedded Perl as a workaround. But I would like to use json extraction
without embedded Perl.


--
Best wishes Victor Ustugov
mailto:victor@corvax.kiev.ua
public GnuPG/PGP key: https://victor.corvax.kiev.ua/corvax.asc

--
## subscription configuration (requires account):
## https://lists.exim.org/mailman3/postorius/lists/exim-users.lists.exim.org/
## unsubscribe (doesn't require an account):
## exim-users-unsubscribe@???
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/