.new
.vitem "&*${extract json{*&<&'key'&>&*}{*&<&'string1'&>&*}{*&<&'string2'&>&*}&&&
{*&<&'string3'&>&*}}*&"
+.vitem "&*${extract jsons{*&<&'key'&>&*}{*&<&'string1'&>&*}{*&<&'string2'&>&*}&&&
+ {*&<&'string3'&>&*}}*&"
.cindex "expansion" "extracting from JSON object"
.cindex JSON expansions
The key and <&'string1'&> are first expanded separately. Leading and trailing
The braces, commas and colons, and the quoting of the member name are required;
the spaces are optional.
Matching of the key against the member names is done case-sensitively.
-If a returned value is a JSON string, it retains its leading and
+For the &"json"& variant,
+if a returned value is a JSON string, it retains its leading and
trailing quotes.
+For the &"jsons"& variant, which is intended for use with JSON strings, the
+leading and trailing quotes are removed.
. XXX should be a UTF-8 compare
The results of matching are handled as above.
.new
.vitem "&*${extract json{*&<&'number'&>&*}}&&&
{*&<&'string1'&>&*}{*&<&'string2'&>&*}{*&<&'string3'&>&*}}*&"
+.vitem "&*${extract jsons{*&<&'number'&>&*}}&&&
+ {*&<&'string1'&>&*}{*&<&'string2'&>&*}{*&<&'string3'&>&*}}*&"
.cindex "expansion" "extracting from JSON array"
.cindex JSON expansions
The <&'number'&> argument must consist entirely of decimal digits,
Field selection and result handling is as above;
there is no choice of field separator.
-If a returned value is a JSON string, it retains its leading and
+For the &"json"& variant,
+if a returned value is a JSON string, it retains its leading and
trailing quotes.
+For the &"jsons"& variant, which is intended for use with JSON strings, the
+leading and trailing quotes are removed.
.wen
uschar *sub[3];
int save_expand_nmax =
save_expand_strings(save_expand_nstring, save_expand_nlength);
- enum {extract_basic, extract_json} fmt = extract_basic;
+ enum {extract_basic, extract_json, extract_jsons} fmt = extract_basic;
while (isspace(*s)) s++;
if (*s != '{') /*}*/
if (Ustrncmp(s, "json", 4) == 0)
- {fmt = extract_json; s += 4;}
+ if (*(s += 4) == 's')
+ {fmt = extract_jsons; s++;}
+ else
+ fmt = extract_json;
/* While skipping we cannot rely on the data for expansions being
available (eg. $item) hence cannot decide on numeric vs. keyed.
if (*p == 0)
{
field_number *= x;
- if (fmt != extract_json) j = 3; /* Need 3 args */
+ if (fmt == extract_basic) j = 3; /* Need 3 args */
field_number_set = TRUE;
}
}
break;
case extract_json:
+ case extract_jsons:
{
uschar * s, * item;
const uschar * list;
}
}
}
+
+ if (fmt == extract_jsons)
+ if (!(lookup_value = dewrap(lookup_value, US"\"\"")))
+ {
+ expand_string_message =
+ string_sprintf("%s wrapping string result for extract jsons",
+ expand_string_message);
+ goto EXPAND_FAILED_CURLY;
+ }
}
/* If no string follows, $value gets substituted; otherwise there can
${extract json {2} {[116, 943, 234, 38793]} }
${extract json {2} {${extract json{IDs} {\{"other":"foo", "IDs": [116, 943, 234]\} }}} }
+${extract json {2} {["red", "green", "blue", "black"]} }
+${extract jsons{2} {["red", "green", "blue", "black"]} }
+
${extract json {seconds} { \{"hours":0, "mins":0, "seconds":59\} }}
${extract json {seconds} {${extract json {2} { ["irrelevant", \{"hours":0, "mins":0, "seconds":59\}] }}}}
> 943
> 943
>
+> "green"
+> green
+>
> 59
> 59
>
> {"1":116, "2":943, "3":234}
> expect: {"1":116, "2":943, "3":234}
>
-> Failed: missing '"' wrapping string key for extract json
+> <>
> expect: <>
>
>