-
Notifications
You must be signed in to change notification settings - Fork 0
/
making_JMESPath_mapping_queries_with_code-davinci-002.txt
391 lines (359 loc) · 11.6 KB
/
making_JMESPath_mapping_queries_with_code-davinci-002.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
The mapping function uses this code:
<code>
import { decorate } from '@daz.is/jmespath';
import * as uuid from 'uuid';
import {
isString, find, get, omit, pick, pickBy, zip, toPairs, fromPairs, pad, padStart, padEnd, uniqBy,
uniq, includes, filter, isNull, isNumber, set, findIndex
} from 'lodash-es';
import {DateTime} from 'luxon';
import {parse as parseQueryString, stringify as asQueryString} from 'qs';
import stringify from 'json-stringify-safe';
import * as showdown from 'showdown';
import { Buffer } from 'buffer';
// Type constants used to define functions.
const TYPE_NUMBER = 0;
const TYPE_ANY = 1;
const TYPE_STRING = 2;
const TYPE_ARRAY = 3;
const TYPE_OBJECT = 4;
const TYPE_BOOLEAN = 5;
const TYPE_EXPREF = 6;
const TYPE_NULL = 7;
const TYPE_ARRAY_NUMBER = 8;
const TYPE_ARRAY_STRING = 9;
const search = decorate({
get: {
_func: ([o, s, d]) => get(o, s, d),
_signature: [{types: [TYPE_OBJECT, TYPE_ARRAY]}, {types: [TYPE_STRING]}, { types: [TYPE_ANY], optional: true}]
},
set: {
_func: ([o, p, v]) => set(o, p, v),
_signature: [{types: [TYPE_OBJECT, TYPE_ARRAY]}, {types: [TYPE_STRING]}, { types: [TYPE_ANY] }]
},
findIndex: {
_func: ([o, v]) => findIndex(o, v),
_signature: [{types: [TYPE_ARRAY]}, {types: [TYPE_OBJECT]}]
},
uuid: {
_func: ([name, NAMESPACE_STRING]) => {
if (name) {
const NAMESPACE_UUID = uuid.v5(NAMESPACE_STRING || 'https://app.kendra.io', uuid.v5.URL);
return uuid.v5(name, NAMESPACE_UUID);
} else {
return uuid.v4();
}
},
_signature: [{ types: [TYPE_STRING], optional: true}, { types: [TYPE_STRING], optional: true}]
},
toLower: {
_func: ([s]) => s.toLowerCase(),
_signature: [{types: [TYPE_STRING]}]
},
replace: {
_func: ([s, a, b]) => s.replace(a, b),
_signature: [{types: [TYPE_STRING]}, {types: [TYPE_STRING]}, {types: [TYPE_STRING]}]
},
trim: {
_func: ([s]) => s.trim(),
_signature: [{types: [TYPE_STRING]}]
},
now: {
_func: () => new Date().toUTCString(),
_signature: []
},
formatDate: {
_func: ([dateString, formatString]) => DateTime.fromISO(dateString).toFormat(formatString),
_signature: [{types: [TYPE_STRING]}, {types: [TYPE_STRING]}]
},
omit: {
_func: ([o, a]) => omit(o, ...a),
_signature: [{types: [TYPE_OBJECT]}, {types: [TYPE_ARRAY_STRING]}]
},
pick: {
_func: ([o, a]) => pick(o, ...a),
_signature: [{types: [TYPE_OBJECT]}, {types: [TYPE_ARRAY_STRING]}]
},
split: {
_func: ([o, s]) => o.split(s),
_signature: [{types: [TYPE_STRING]}, {types: [TYPE_STRING]}]
},
find: {
_func: ([o, k, v]) => find(o, o2 => o2[k] === v),
_signature: [{types: [TYPE_ARRAY]}, {types: [TYPE_STRING]}, {types: [TYPE_ANY]}]
},
compact: {
_func: ([o]) => pickBy(o),
_signature: [{types: [TYPE_OBJECT]}]
},
qs: {
_func: ([o]) => asQueryString(o),
_signature: [{types: [TYPE_OBJECT]}]
},
parseQs: {
_func: ([s]) => parseQueryString(s),
_signature: [{types: [TYPE_STRING]}]
},
zip: {
_func: ([a1, a2]) => zip(a1, a2),
_signature: [{types: [TYPE_ARRAY]}, {types: [TYPE_ARRAY]}]
},
debug: {
_func: ([v]) => {
console.log('debug value in mapping', v);
return v;
},
_signature: [{types: [TYPE_ANY]}]
},
json: {
_func: ([v]) => stringify(v),
_signature: [{types: [TYPE_ANY]}]
},
markdown: {
_func: ([s]) => {
showdown.setFlavor('github');
const converter = new showdown.Converter();
return converter.makeHtml(s);
},
_signature: [{types: [TYPE_STRING]}]
},
btoa: {
_func: ([s]) => btoa(s),
_signature: [{types: [TYPE_STRING]}]
},
base64encode: {
// btoa is not compatible with UTF-8, to safely manage the base64encoding
_func: ([s]) => Buffer.from(s).toString('base64'),
_signature: [{types: [TYPE_STRING]}]
},This
pairwise: {
_func: ([inputArray]) => {
return inputArray.reduce((a, v, i, src) => {
a.push({ current: v, next: src[i + 1] || null });
return a;
}, []);
},
_signature: [{types: [TYPE_ARRAY]}]
},
numDiff: {
_func: ([a, b]) => {
return a - b;
},
_signature: [{ types: [TYPE_NUMBER] }, { types: [TYPE_NUMBER] }]
},
percentChange: {
_func: ([oldValue, newValue]) => {
return ((newValue - oldValue) / oldValue) * 100;
},
_signature: [{ types: [TYPE_NUMBER] }, { types: [TYPE_NUMBER] }]
},
// Takes an array of objects and returns an object with values are array
// calculated by grouping values based on keys
groupByKeys: {
_func: ([inputArray]) => {
return inputArray.reduce((o, v) => {
Object.keys(v).forEach(key => {
if (o[key]) {
o[key].push(v[key]);
} else {
o[key] = [v[key]];
}
});
return o;
}, {});
},
_signature: [{types: [TYPE_ARRAY]}]
},
toPairs: {
_func: ([o]) => toPairs(o),
_signature: [{types: [TYPE_OBJECT]}]
},
fromPairs: {
_func: ([a]) => fromPairs(a),
_signature: [{types: [TYPE_ARRAY]}]
},
pad: {
_func: ([s, l, c]) => pad(s, l, c),
_signature: [{types: [TYPE_STRING]}, {types: [TYPE_NUMBER]}, {types: [TYPE_STRING]}]
},
padStart: {
_func: ([s, l, c]) => padStart(s, l, c),
_signature: [{types: [TYPE_STRING]}, {types: [TYPE_NUMBER]}, {types: [TYPE_STRING]}]
},
padEnd: {
_func: ([s, l, c]) => padEnd(s, l, c),
_signature: [{types: [TYPE_STRING]}, {types: [TYPE_NUMBER]}, {types: [TYPE_STRING]}]
},
product: {
_func: ([a, b]) => a * b,
_signature: [{types: [TYPE_NUMBER]}, {types: [TYPE_NUMBER]}]
},
currency: {
_func: ([n, l, c]) => new Intl.NumberFormat(l, { style: 'currency', currency: c }).format(n),
_signature: [{types: [TYPE_NUMBER, TYPE_STRING]}, {types: [TYPE_STRING]}, {types: [TYPE_STRING]}]
},
uniq: {
_func: ([a]) => uniq(a),
_signature: [{types: [TYPE_ARRAY]}]
},
uniqBy: {
_func: ([a, e]) => uniqBy(a, e),
_signature: [{types: [TYPE_ARRAY]}, {types: [TYPE_STRING]}]
},
includes: {
_func: ([a, i]) => includes(a, i),
_signature: [{types: [TYPE_ARRAY, TYPE_OBJECT, TYPE_STRING]}, {types: [TYPE_ANY]}]
},
all: {
_func: ([a]) => filter(a).length === a.length,
_signature: [{types: [TYPE_ARRAY]}]
},
parseDate: {
_func: ([n]) => {
if (isNull(n)) {
return null;
}
if (isNumber(n)) {
const nn = (n - 25569) * 86400;
return DateTime.fromSeconds(nn).toISO();
}
if (isString(n)) {
return DateTime.fromISO(n).toISO();
}
return null;
},
_signature: [{types: [TYPE_NUMBER, TYPE_STRING, TYPE_NULL]}]
},
parseDuration: {
_func: ([n]) => {
if (isNull(n)) {
return null;
}
if (isString(n) && n.includes(':')) {
const [m, s] = n.split(':');
return (parseInt(m, 10) * 60) + s;
}
if (isString(n)) {
return parseInt(n, 10);
}
if (isNumber(n)) {
return n;
}
return null;
},
_signature: [{types: [TYPE_NUMBER, TYPE_STRING, TYPE_NULL]}]
}
});
export function mappingUtility(value, expr) {
return search(expr)(value);
}
</code>
Default values
^^^^^^^^^^^^^^^
Set default values by using "||" (or)
- value || '[default]'
Filtering data
^^^^^^^^^^^^^^
Filter by the existence of a flag
.. code-block:: text
data[?tags[?@ == 'deleted']]
This will keep any item that has a "deleted" flag in the "tags" array. More useful in this instance would be it's inverse.
.. code-block:: text
data[?!(tags[?@ == 'deleted'])]
Adding a new key to an object
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If you're wrangling data from one form to another, you may need to add new keys.
.. code-block:: text
data[*].merge(@, { key: value })
Setting a value based on a flag
"""""""""""""""""""""""""""""""
If you want to add a new value to your object based on whether or not another flag is set in an array, you can do something like this.
.. code-block:: text
data[*].merge(@,{deleted: @.tags[?@ == `deleted`]})
This will look through the tags attribute to see whether or not a "deleted" tag is set, and set the new attribute accordingly.
Accessing context from within a merge
"""""""""""""""""""""""""""""""""""""
When using a merge, or similar function, paths become relative to your current item.
If you need to access a value from beyond this context, you need to use the "$" operator to access the root of the data.
.. code-block:: text
data[*].merge(@,{
exists:contains($.context.flowsExisting || [''], join('-',[@.adapterName,@.workflowId]))
})
Finding a specific key in an array
"""""""""""""""""""""""""""""""""""
A more advanced version of the object merge can involve looking up a specific entry in an existing array of objects.
In this example, we merge two object arrays, and look up a unique key from a list, and then add that key so that we can create a reference.
.. code-block:: text
data && merge(data, {track: uniqBy(data.track, 'key')})
.. code-block:: text
find($.data.licensor, 'name', "LICENSOR NAME").id,
Extracting content from different levels in a nested array
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
This example looks through an array of objects, extracts the "name", at the top level
and then pulls out all the "config" values from an array of properties
.. code-block:: text
data[*].[name, @.properties[*].[config][] ]
This can also filter content based on properties
.. code-block:: text
data[*].[name, properties[?type==`Object` || type==`List` || type==`ObjectReference` || type==`ListReference`].[config][]]
And this version will return an array of objects
.. code-block:: text
data[*].{parent: name, children: properties[?type==`Object` || type==`List` || type==`ObjectReference` || type==`ListReference`].[config][]}
Merging two arrays
^^^^^^^^^^^^^^^^^^^
If you have two arrays, and want to combine them, you can use the flatten operator "[]".
For example, if you want to combine information from a form with data you've previously saved to context, you can merge the two arrays like this.
.. code-block:: text
[context.saved, [data]][]
Creating an object from two arrays - spreadsheet import
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
To import from a spreadsheet, converting each row into an object with keys in the first row,
you need a combination of actions.
.. code-block:: text
data && data.Sheet1[1:].[$.data.Sheet1[0], @].map(&fromPairs(zip([0], [1])), @)
- 'data.Sheet1[1:]' skips the first row, which is the header row
- '[$.data.Sheet1[0], @]' for each row, take the first row and the current row
- 'map([expression],[elements])' apply an expression to every row, passing in the current row (@)
- '&fromPairs(..)' convert an array of arrays into an object
- 'zip([0], [1])' zip the first row and the current row together,
creating an array of arrays with each column converted to [key, value] arrays
Converting an array of results to a JSON Schema fragment
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Given data like this:
.. code-block:: text
[
{
"name": "Acme",
"id": 1
},
{
"name": "Widgets Inc",
"id": 2
},
{
"name": "Foo Bar",
"id": 3
}
]
We can generate this:
{
"oneOf": [
{
"title": "Dave's Fish",
"type": "integer",
"enum": [
16
]
},
{
"title": "Chip More",
"type": "integer",
"enum": [
30
]
}
]
}
.. code-block:: text
data[*].{ title: name, type: 'integer', enum: [id] }
</code>