DEV Community


Posted on • Updated on

JSON parser

This is a JSON parser which allows the user to find the value of a key which is present inside the JSON structure. The question is why do I need a JSON parser? I can do that by writing a loop. Why do I need an additional parser which does that for me?

Well in my last project I used to have a class ProjectUtils which contained functions of commonly used things. That is where I got the inspiration to come up with this.

This parser is used to will retrieve a key from a JSON structure provided you give the path to where the key is present.

let us take the below JSON structure as an example.

    "perms": [{
        "changeDefault": "Yes",
        "visible": true,
        "notused": true,
        "testArray": [{
            "TestKey1": "TestVal1",
            "TestKey2": "TestVal2",
            "TestKey3": "TestVal3",
            "TestKey4": "TestVal4"
        "selectable": true,
        "allAccess": true,
        "defaultOnly": true
    "avatarUrl": "",
    "cartId": "8ladf51ds65ga6",
    "DeviceDetails": {
        "DeviceName": "MotoXPlay",
        "UDID": "459f8202b2n92h1",
        "Info": {
            "IPDetails": "",
            "TestMgmtID": "8080",
            "IsPhone": true
Enter fullscreen mode Exit fullscreen mode

If I want to get the value of key TestKey4, I would be able to do it by passing the JSON structure and then the location of where the value is present.

getNodeValue(JSONStructure, perms.0.testArray.0.TestKey4);

Where the first parameter is the JSONStructure to search.
The second parameter is the location. As per the example perms is name of the array, the 0th index, then the testArray and 0th index and the Key TestKey4.

If I wanted to get an object, then I would not need to pass the index. If I wanted to check if the isPhone is true of false. I could get it like

getNodeValue(JSONStructure, DeviceDetails.Info.IsPhone);

As per the current implementation the getNodeValue function would return an Java Object. The advantage of returning as an Object is that whatever type of value the key holds, the user could get that without anticipating the return value.

This is the GitHub repo
Leave an comment if guys think this will be helpful. Hope it helps.


Top comments (0)