How to use the jsonpath/jsonpath.min.query function in jsonpath

To help you get started, we’ve selected a few jsonpath examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github JeanBaptisteWATENBERG / react-jsonpath-editor / src / components / suggestionBuilder.js View on Github external
export const getSuggestions = (jsonPath, caretPosition, jsonToTestAgainst) => {
    try {
        if (caretPosition !== jsonPath.length) {
            throw new Error('Force to eval options according to carret positions');
        }
        const filteredJson = jp.query(jsonToTestAgainst, jsonPath)[0];
        if (Array.isArray(filteredJson)) {
            return suggestions.filter(s => s.scopes.includes('array'));
        } else {
            // Get last jsonpath to offer available suggestions 
            const parentJsonPath = jsonPath.substring(0, jsonPath.lastIndexOf('.') + 1);
            // Compute extra json path value, to be used to filter available suggestions
            const filterSuggestionJsonPath = jsonPath.substring(jsonPath.lastIndexOf('.') + 1);
            let additionalSuggestions = [];
            if (parentJsonPath) {
                const appliableScopes = suggestions.filter(s => parentJsonPath.endsWith(s.value));
                additionalSuggestions = guessSuggestionsFromScopes(appliableScopes, caretPosition, parentJsonPath, jsonToTestAgainst);
            }
            return [...additionalSuggestions.filter(s => s.value.includes(filterSuggestionJsonPath) && s.value !== filterSuggestionJsonPath), ...suggestions.filter(s => s.scopes.includes('object'))];
        }
    } catch (e) {
        const appliableScopes = suggestions.filter(s => {
github JeanBaptisteWATENBERG / react-jsonpath-editor / src / components / suggestionBuilder.js View on Github external
return suggestions.map(s => {
        if (s.value === 'all_properties') {
            const jsonPathToObject = jsonPath.substring(0, jsonPath.length - 1);
            try {
                const filteredJson = jp.query(jsonToTestAgainst, jsonPathToObject)[0];

                const properties = Object.keys(filteredJson);

                return properties.map(p => ({
                    value: p,
                    description: 'property',
                    scopes: ['object']
                }));
            } catch (e) {
                // ignore error
                return [];
            }


        } else if (s.value === 'all_properties_recursively') {
            const jsonPathToObject = jsonPath.substring(0, jsonPath.length - 2);
github JeanBaptisteWATENBERG / react-jsonpath-editor / src / components / suggestionBuilder.js View on Github external
} catch (e) {
                // ignore error
                return [];
            }
        } else if (s.value === 'all_properties_of_parent_array') {
            //"$.d[1].df.f[?(@.t === 'tf' && @.{cursorHere}truc === 'd' && @.defined)]"
            // 1. split at cursor, take first part
            //"$.d[1].df.f[?(@.t === 'tf' && @."
            const splittedJsonPathAtCursor = jsonPath.substring(0, carretPosition);
            // 2. Search for last "[" index, then split at this index, take first part
            //"$.d[1].df.f" 
            const lastIndexOfOpennedArray = splittedJsonPathAtCursor.lastIndexOf('[');
            const jsonPathToParentArray = splittedJsonPathAtCursor.substring(0, lastIndexOfOpennedArray);
            // 3. We then want to evaluate all unique properties in f array at first level 
            try {
                const filteredJson = jp.query(jsonToTestAgainst, jsonPathToParentArray)[0];
                const properties = Array.from(new Set(getAllPropertiesAtFirstLevel(filteredJson)));
                return properties.map(p => ({
                    value: p + ' ',
                    setCarretAt: p.length-1,
                    description: 'property',
                    scopes: ['object']
                }));
            } catch(e) {
                return [];
            }
        } else {
            return s;
        }
    });
};

jsonpath

Query JavaScript objects with JSONPath expressions. Robust / safe JSONPath engine for Node.js.

MIT
Latest version published 4 years ago

Package Health Score

70 / 100
Full package analysis