Scenario I am looking to find a path such as myObject.authors[2].name
by starting at a dynamic point in a JSON object (think of it as a string). Take the following, for example:
I have this JSON:
var myOject = {
"title":"My New Post",
"authors":[
{
"name":"Smith",
"description":"Name is smith"
},
{
"name":"Jones",
"description":"Name is {{#reverseParse name}}"
},
{
"name":"Lincoln",
"description":"Name is Lincoln"
}
]
}
And I would like to use a custom handlebars.js helper or JS function to find dynamic attributes such as the name: {{#reverseParse **name**}}
. So it would replace {{#reverseParse name}}
with Jones
.
Potential Solutions
I would be surprised if there isn't a pre-existing solution to this since it seems like it would be a common encounter. However, I've been unable to find some. Are there any?
I thought about just creating a reverse parser that would find the function name reverseParse
and start parsing the string backwards and construct the path as it goes.
Q How can I get the JSON path from a dynamic variable in the same JSON (think of it as a string)?
PS - let me know if I can clarify in any way. Thanks!
EDIT
An example function signature could be something like this:
var reverseParse = function([string] myString) {
var jsonPath;
...
// find string position of {{#reverseParse [variable]}}
// start parsing the string from that position to the left
// as you encounter JSON characters, acknowledge and construct path
// etc?
return jsonPath;
}
where the string argument should actually be JSON formatted.