Git Product home page Git Product logo

sfdc-json-parser's Introduction

SFDC-JSON-Parser

A JSON parsing abstraction built over the existing JSONParser in Salesforce to simplify extracting values (no need to create a class to serialise to).

I built this when making many HTTP callouts to an API returning many different JSON formatted responses. I only wanted one or two values from each response so it made no sense to build classes for these responses. Using the basic JSONParser in Salesforce is a nightmare iterating through the different tokens, working out where you are in the token tree.

So now you can just create this serialiser and call get() to find your key in the JSON object. get() only does a shallow scan of the parent object and always returns a JSONField. You then access the value from the Value property (Value is null if the key was not found). If your key is multiple objects deep then you can recursively call get() to access the key walking down the object tree. If the value for a key is an object or array then it will be returned as a string.

If you need to access an array, first return the array string from your key. Then call Util_JSONParser.parseArray() passing in your array as a string. This method returns a list of Util_JSONParser. You can then iterate through these parsers, accessing the keys as usual.

###Usage:

String jsonContent = '{' +
					 '  "someKey": "someValue",' +
					 '  "anotherKey": true,' +
					 '  "aKeyWithObject": {' +
					 '    "moreValues": "foo"' +
					 '  },' +
					 '  "anArray": [' +
					 '    {' +
					 '      "bar": true,' +
					 '      "baz": "yes"' +
					 '    },' +
					 '    {' +
					 '      "bar": false,' +
					 '      "baz": "banana"' +
					 '    }' +
					 '  ]' +
					 '}';
					 
Util_JSONParser parser = Util_JSONParser.createParser(jsonContent);
String value1 = parser.get('someKey').Value; // returns 'someValue'
String value2 = parser.get('anotherKey').Value; // returns 'true'
String value3 = parser.get('notPresentKey').Value; // returns Null
String value4 = parser.get('aKeyWithObject').get('moreValues').Value; // returns 'foo'
String value5 = parser.get('aKeyWithObject').Value; // returns '{"moreValues":"foo"}'

String arrayString = parser.get('anArray').Value; // returns '[{"bar":true,"baz":"yes"},{"bar":false,"baz":"banana"}]'
List<Util_JSONParser> arrayParser = Util_JSONParser.parseArray(arrayString);
for (Util_JSONParser p : arrayParser) {
	String value6 = p.get('bar').Value; // returns 'true' for first object, 'false' for second
}

sfdc-json-parser's People

Contributors

trigger2991 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.