0

I have the following data body:

{
    "recipients": [
        {
            "recipient": "test"
        }
    ],
    "data": "Text message from client",
    "chatType": "single"
}

And, I have this string in my database which we need to use for integration purposes.

"body.recipients[0].recipient"

Is there a way to extract the data requested from this string? I tried with dot notation, also tried to split the string and convert it into an object, but I got stuck on the index id.

The response expected in this case is: "test".

Split the message is not a problem. But i don't know how convert the string index [0] to a valid string.

Maykel Esser
  • 289
  • 3
  • 15
  • 1
    if that object was stored in the `body` variable, you could pass that string to `eval()`. Anyway this is a comment that will turn on a flame... another option would be to parse that string and find the coordinates. Like using `string.split('.')` and looping over the fragments to address the respective property name in the object – Diego D Feb 06 '23 at 13:10
  • Splitting the string using `.` and then using `Array.prototype.reduce` is the safer way :) – Terry Feb 06 '23 at 13:16
  • @DiegoD this is what i thought in the first time. I could reach into the dots, but when i faced with the index part (recipients[0]), it gets me nuts, since it cant detect the [0] as an valid index since it is a string part – Maykel Esser Feb 06 '23 at 13:19
  • @Terry got the same problem as i talked to DiegoD. When i need to "convert" the indexes, i dont know how to do that, even with a for, foreach, reduce, etc... – Maykel Esser Feb 06 '23 at 13:20
  • @Diego D: haha I like how you summoned the flames. Indeed, this would be the perfect time to use eval()... But since this is some backend ("database", "chat"...) it's definitely not a good idea. – Foxcode Feb 06 '23 at 13:24
  • @Foxcode there are some untouchable taboos.. I'd say eval and regex over html are the two biggest ones! I went too far even just hinting at that!! luckily far more options came out :) The point is that it's always dangerous to take that path – Diego D Feb 06 '23 at 13:31
  • 1
    You can use this function: https://jsbin.com/vanaroz/edit?js,console but this version needs `keyStr` to be passed in this way: `recipients[0].recipient` (there is no `body`) – Alizadeh118 Feb 06 '23 at 13:33
  • 1
    @MaykelEsser since I'm not sure the solution targets also the arrays... here's what I came up with before the question was closed. https://jsfiddle.net/dzrc7vy5/ – Diego D Feb 06 '23 at 13:34
  • 1
    @DiegoD: Was scratching my head over the mention of regex. Didn't even think about regex being able to execute malicious code... Hmm, at least something was learned here https://stackoverflow.com/questions/4579497/is-there-any-way-to-put-malicious-code-into-a-regular-expression if someome is interested. Is this kinda what you meant? – Foxcode Feb 06 '23 at 13:39
  • @Foxcode no I didn't mean that and it's interesting to know also that fact actually! I just meant that very often people ask how to parse html with regex.. and it will immediately translate to a ton of downvotes because the short answer is: you can't. – Diego D Feb 06 '23 at 13:41
  • @DiegoD your approach has solved the issue <3 i'll get deep into it to understand how you make it! Many Thanks! – Maykel Esser Feb 06 '23 at 13:43
  • @Diego D: Oh I can see the downvotes part. But that's usually not because something is impossible (almost everything is with regex. I painfully learned recently), it's just that it's usually a near-unreadable mess (even the correct solutions ^^')... but mostly downvotes occur because of badly phrased questions, as I think you and I know. :) – Foxcode Feb 06 '23 at 13:45
  • 1
    @MaykelEsser I'm glad.. anyway it's fair to say that the other solution given by another user was also valid I think.. I didn't check actually buy I supposed. It was also using `reduce` afair, and it was more elegant in that regard. Anyway you solved your problem. – Diego D Feb 06 '23 at 13:45
  • 1
    @foxcode look here https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 and enjoy! :D – Diego D Feb 06 '23 at 13:47
  • 1
    @DiegoD: Loool and here I am for years not knowing that there was any humor on stackoverflow (and that humor can get you 4.4k upvotes lul). You just made my day xD. (And yes sorry mods, you can delete this useless comment and I'll take the downvotes. This was worth it. And imo highly educational.) – Foxcode Feb 06 '23 at 13:58
  • @Alizadeh118 your solution works as well :) – Maykel Esser Feb 06 '23 at 14:00

0 Answers0