I have a property, that is unfortunately an id, so it's difficult for me to get the further properties :

{
   "myprop1":1,
   "myprodid_121212":
   {
      "prop1":1,
      "prop2":2
   }
}

Is it possible with JSON Path to get for example the second property (without knowing the property name)? myprodid...

1 Answers

0
user1514042 On

Here's how you do it using ES6:

const [key, value] = Object.entries({
        "myprop1": 1,
        "myprodid_121212":
        {
            "prop1": 1,
            "prop2": 2
        }
    }).find((entry, index) => index === 1);

you can easily check for the property name too by examining entry, see example below:

const [key, value] = Object.entries({
        "myprop1": 1,
        "myprodid_121212":
        {
            "prop1": 1,
            "prop2": 2
        }
    }).find(([key], index) => key.split("_")[0] === "myprodid");