# Parse JSON Path

**URL:** <https://forum.inductiveautomation.com/t/parse-json-path/55125>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [January 18, 2022, 5:41pm UTC](https://forum.inductiveautomation.com/t/parse-json-path/55125 "2022-01-18T17:41:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kgamble](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgamble/32/8019_2.png) [@kgamble](https://forum.inductiveautomation.com/u/kgamble)\
**Post date:** [January 18, 2022, 5:41pm UTC](https://forum.inductiveautomation.com/t/parse-json-path/55125/1 "2022-01-18T17:41:42Z")

</div>

I need to parse a JSON path in a format similar to how it is done with the JSON functions in MSSQL. However I don’t really want to add the external library `jsonpath-ng` to make it happen. I know that GSON is used quite frequently, and with all of perspective being stored in JSON, is this something that a built in function already covers?

Example:

I have JSON

```auto
{
  "level1":
  {
    "level2":{
      "finalKey": "value"
    }
  },
  "otherKey":"someValue"
}

```

And I need to get the value for `$.level1.level2.finalKey` to equal “value”

My guess/hope is that there is a function that executes the `jsonGet` expression that I could potentially leverage to extrapolate this.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 18, 2022, 5:52pm UTC](https://forum.inductiveautomation.com/t/parse-json-path/55125/2 "2022-01-18T17:52:41Z")

</div>

Probably the closest first-party would be [`JsonUtilities.read`](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.13/com/inductiveautomation/ignition/common/JsonUtilities.html#read(com.inductiveautomation.ignition.common.gson.JsonElement,java.lang.String)) - but you’ll probably have to do some type coercion from your original JSON (probably raw Python dicts?) to the GSON-based classes we use internally.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 18, 2022, 6:04pm UTC](https://forum.inductiveautomation.com/t/parse-json-path/55125/3 "2022-01-18T18:04:11Z")

</div>

Put together:

```python
from com.inductiveautomation.ignition.common import TypeUtilities, JsonUtilities

d = {
  "level1":
  {
    "level2":{
      "finalKey": "value"
    }
  },
  "otherKey":"someValue"
}

def get(json, path):
	gson = TypeUtilities.pyToGson(json)
	return JsonUtilities.read(gson, path).orElse(None)
	
print get(d, "level1.level2.finalKey")

```

---

<div class="post-metadata">

**Author:** ![kgamble](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgamble/32/8019_2.png) [@kgamble](https://forum.inductiveautomation.com/u/kgamble)\
**Post date:** [January 18, 2022, 6:05pm UTC](https://forum.inductiveautomation.com/t/parse-json-path/55125/4 "2022-01-18T18:05:29Z")

</div>

You are the best Paul! This is perfect.

---

<div class="post-metadata">

**Author:** ![kgamble](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgamble/32/8019_2.png) [@kgamble](https://forum.inductiveautomation.com/u/kgamble)\
**Post date:** [January 18, 2022, 6:08pm UTC](https://forum.inductiveautomation.com/t/parse-json-path/55125/5 "2022-01-18T18:08:58Z")

</div>

Another note, incase you wanted this to be all in python for testing purposes. You could use `reduce` as well to solve this, I just realized this after going through Pauls example.

```auto
from functools import reduce
import operator

def find(element, json):
    return reduce(operator.getitem, element.split('.'), json)
    
d = {
  "level1":
  {
    "level2":{
      "finalKey": "value"
    }
  },
  "otherKey":"someValue"
}

print find("level1.level2.finalKey", d)

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 18, 2022, 6:10pm UTC](https://forum.inductiveautomation.com/t/parse-json-path/55125/6 "2022-01-18T18:10:26Z")

</div>

That will limit you to object keys without allowing array addressing, though. You could split the difference and just use `JsonPath.parse()` and then extract the elements.
