Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A minimum implementation of JSON Path in Apex.

License

NotificationsYou must be signed in to change notification settings

Dogeforce/apex-json-path

Repository files navigation

This repository contains a minimum implementation of theJSON 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:

JSONPathj=newJSONPath('{"name":"John","company":{"name":"Company"}}');StringcompanyName=j.get('$.company.name');System.assertEquals('Company',companyName,'Wrong company name.');

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

JSONPathjpListNested=newJSONPath('[{"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:

JSONPathjpAttributeListFromObjectList=newJSONPath('[{"name":"John"},{"name":"Mary"}]');List<Object>names= (List<Object>)jpAttributeListFromObjectList.get('$[*].name');// names[0] = John and names[1] = 'Mary'JSONPathjpAttributeListFromInnerObjectList=newJSONPath('{"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 andlength 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:

JSONPatharrayFunctions=newJSONPath('{"numbers":[1, 2, 3, 40]}');arrayFunctions.get('$.numbers.empty()');// falsearrayFunctions.get('$.numbers.length()');// 4arrayFunctions.get('$.numbers.size()');// 4arrayFunctions.get('$.numbers.min()');// 1arrayFunctions.get('$.numbers.max()');// 40arrayFunctions.get('$.numbers.avg()');// 11.5

About

A minimum implementation of JSON Path in Apex.

Topics

Resources

License

Stars

Watchers

Forks

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp