Giter VIP home page Giter VIP logo

apex-json-path's Introduction

Apex JSON Path

This repository contains a minimum implementation of the JSON Path Syntax. With this you can access data from a JSON string using a path specified by another string.

Might come in handy with integrations, specially, where one might have to read data from a JSON payload according to some specifications from data or metadata instead of deserializing the whole thing to an Apex type.

Usage

To get an attribute value:

JSONPath j = new JSONPath('{"name":"John","company":{"name":"Company"}}');

String companyName = j.get('$.company.name');

System.assertEquals('Company', companyName, 'Wrong company name.');

It works for returning entire objects. So you could use $.company to get the Object that contains the company data (then you could cast it to a Map<String, Object> and access the name from there if you wanted to). This is also true for returning inner lists.

JSONPath jpListNested = new JSONPath('[{"attr":[{"name":"John"},{"name":"Mary"}]}]');

System.assertEquals(
    'Mary',
    jpListNested.get('$[0].attr[1].name'),
    'Incorrect data.'
);

Also works for returning specific attributes from inner lists. So if the JSON payload is a list or the object contains a list then it is reachable using the [] or [*] syntax:

JSONPath jpAttributeListFromObjectList = new JSONPath(
    '[{"name":"John"},{"name":"Mary"}]'
);
List<Object> names = (List<Object>) jpAttributeListFromObjectList.get('$[*].name');
// names[0] = John and names[1] = 'Mary'

JSONPath jpAttributeListFromInnerObjectList = new JSONPath(
    '{"people":[{"name":"John"},{"name":"Mary"}]}}'
);
names = (List<Object>) jpAttributeListFromInnerObjectList.get('$.people[*].name');
// names[0] = John and names[1] = 'Mary'

List functions

The following functions are available for usage with list attributes:

  1. min to get the minimum value (returns a double);
  2. max to get the maximum value (returns a double);
  3. avg to get the average value (returns a double);
  4. sum to get the sum of values (returns a double);
  5. size and length to get the quantity of values in the list (returns an integer);
  6. empty to get a boolean indicating if the list is empty;

Usage:

JSONPath arrayFunctions = new JSONPath('{"numbers":[1, 2, 3, 40]}');

arrayFunctions.get('$.numbers.empty()');  // false
arrayFunctions.get('$.numbers.length()'); // 4
arrayFunctions.get('$.numbers.size()');   // 4
arrayFunctions.get('$.numbers.min()');    // 1
arrayFunctions.get('$.numbers.max()');    // 40
arrayFunctions.get('$.numbers.avg()');    // 11.5

apex-json-path's People

Contributors

jamessimone avatar renatoliveira avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.