-4

Please note that this is not a json object :) My string is: {"message":"***error in SAP module:-1***","status":400} This is not a json object, this is a pure string. I cannot turn it into a json object due to technical limitations.

So, I want to take only the bold string (all the value of "message"). I thought about lastIndexOf, and pick the string between ":" and "," But I got messed up with the escape characters for the quotes. How can I achieve it with lastIndexOf? Or in another better way?

nilsandrey
  • 1,030
  • 11
  • 28
Erez
  • 502
  • 3
  • 6
  • 17
  • 1
    There is no such thing as a JSON Object. Objects are not JSON. JavaScript Object Notation, is a string format for describing an object. You just need to parse it. – Taplar Dec 21 '20 at 17:50
  • 1
    If it is a string, then it *is* JSON. JavaScript objects are just that, and should better not be named "json object". So you have used the term in the opposite way than intended. – trincot Dec 21 '20 at 17:51
  • 1
    `JSON.parse('{"message":"error in SAP module:-1","status":400}').message` and done – Taplar Dec 21 '20 at 17:52
  • 1
    Please elaborate on what the "technical limitations" are. – trincot Dec 21 '20 at 17:52
  • It **looks** like the string is valid JSON already. – Quentin Dec 21 '20 at 17:53
  • Answering here because marked as duplicate. Maybe this could help in your specific case: `result = str.split(/{"message":"(.*)\:[-\d]*\"/m)[1];` – nilsandrey Dec 21 '20 at 18:00

1 Answers1

0

If you can't use JSON.parse, I would use a regex to read it. You know you want the string directly following "message":", so I would look for that, then grab everything from there to the next occurrence of "