There is such a problem, you need to pass the JSON path to the function as a string, for example:

var json_str = "JSON.prop1.prop2.value"; 

and in the end I need to get it back to normal. You can of course parse regular files and compile it, but it may have some other method, eval does not offer, I have 'use strict'.
The main task is a function that, however, the input receives JSON as a string, and it must return the value "value", or "false" if there is no "value"

  • one
    What have you done to address this issue? look towards String#split() and Array#reduce() - NumminorihSF
  • eval works fine with use strict . Although it does not affect the fact that he is out of place here. - Qwertiy
  • @Qwertiy is very appropriate, it is enough to make eval ("JSON.prop1.prop2.value") and using catc'a to catch the error there is some kind of prop not - pnp2000
  • Try to write more detailed questions. To get an answer, explain exactly what you see the problem, how to reproduce it, what you want to get as a result, etc. Give a sample code. - Grundy
  • @Grundy described the problem more fully deployed - pnp2000

1 answer 1

 var obj = { prop1: { prop2: { value: 14 } } }; var keys = "JSON.prop1.prop2.value"; var value = obj; for (var key of keys.split(".").slice(1)) value = value[key]; console.log(value); 

  • yes here that in that it is impossible to take var value = obj; - pnp2000
  • @ vnn198, why not? Add your question with all the necessary data - Grundy
  • Well, that is the task;), if there is an external object, then everything is quite simple, this is where I have a snag that there is no stone flower without an object, I feel that everything goes to eval - pnp2000
  • one
    @ vnn198, without an object and eval will not help. If you mean that the object is global - then use window as the initial object - Grundy
  • this is the catch, I have a node, and the object is not necessarily global - pnp2000