MCPcopy Index your code
hub / github.com/josdejong/jsoneditor / createQuery

Function createQuery

src/js/jmespathQuery.js:12–65  ·  view source on GitHub ↗
(json, queryOptions)

Source from the content-addressed store, hash-verified

10 * @return {string} Returns a query (as string)
11 */
12export function createQuery (json, queryOptions) {
13 const { sort, filter, projection } = queryOptions
14 let query = ''
15
16 if (filter) {
17 const examplePath = filter.field !== '@'
18 ? ['0'].concat(parsePath('.' + filter.field))
19 : ['0']
20 const exampleValue = get(json, examplePath)
21 const value1 = typeof exampleValue === 'string'
22 ? filter.value
23 : parseString(filter.value)
24
25 query += '[? ' +
26 filter.field + ' ' +
27 filter.relation + ' ' +
28 '`' + JSON.stringify(value1) + '`' +
29 ']'
30 } else {
31 query += Array.isArray(json)
32 ? '[*]'
33 : '@'
34 }
35
36 if (sort) {
37 if (sort.direction === 'desc') {
38 query += ' | reverse(sort_by(@, &' + sort.field + '))'
39 } else {
40 query += ' | sort_by(@, &' + sort.field + ')'
41 }
42 }
43
44 if (projection) {
45 if (query[query.length - 1] !== ']') {
46 query += ' | [*]'
47 }
48
49 if (projection.fields.length === 1) {
50 query += '.' + projection.fields[0]
51 } else if (projection.fields.length > 1) {
52 query += '.{' +
53 projection.fields.map(value => {
54 const parts = value.split('.')
55 const last = parts[parts.length - 1]
56 return last + ': ' + value
57 }).join(', ') +
58 '}'
59 } else { // values.length === 0
60 // ignore
61 }
62 }
63
64 return query
65}
66
67/**
68 * Execute a JMESPath query

Callers 1

tryCreateQueryFunction · 0.85

Calls 3

parsePathFunction · 0.90
getFunction · 0.90
parseStringFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…