Home > front end >  JavaScript Isolate JSON object inside a String
JavaScript Isolate JSON object inside a String

Time:11-26

I am looking for some help regarding an issue I am facing within an Azure Logic App. I have a JSON output provided by a failed HTTP request shown beneath:

{
  "error": {
    "code": "",
    "message": "Http request failed with statusCode=BadRequest : {\"error\":{\"code\":\"ValidationFailed\",\"message\":\"Validation of indicator content failed.\",\"target\":\"body\",\"details\":[{\"code\":\"ValidationFailed\",\"message\":\"The value 1 is not a valid domain name.\",\"target\":\"domainName\"}]}}; ",
    "innerError": {
      "date": "2021-11-25T11:12:09",
      "request-id": "",
      "client-request-id": ""
    }
  }
}

What I intend on doing is isolating the "message" value to inform users of my app on why the app is failing. I believe the value of "message" is a string, however, it also contains a JSON object. If I could escape the characters either side of the object, I could parse the JSON and select the values within that object.

This is a better view of the object I want to be able to access:

{
  "error": {
    "code": "ValidationFailed",
    "message": "Validation of indicator content failed.",
    "target": "body",
    "details": [
      {
        "code": "ValidationFailed",
        "message": "The value 1 is not a valid domain name.",
        "target": "domainName"
      }
    ]
  }
}

How can I access these values? I am able to make use of JavaScript within my logic app, so if there is a simple solution through the use of JS, that would be great.

I am currently parsing the initial JSON object, shown at the top of this post, then parsing the "message" values in an attempt to access those variables. This however is not working as the value of "message" is not an object.

If I am able to access the nested "code" and "message" to output the beneath, that would be great.

ValidationFailed: Validation of indicator content failed

Or even better..

ValidationFailed: Validation of indicator content failed - The value 1 is not a valid domain name.

Thank you for any assistance with this. I do not currently have a JavaScript solution, I have attempted to resolve this purely using logic apps. Azure to allow for inline JavaScript to execute within apps so any JavaScript solution will help.

If any more information regarding this is required, please let me know, removing the text in bold beneath prior to parsing the JSON should allow me to make use of the object within the app.

Http request failed with statusCode=BadRequest : `

Leaving the object..

{\"error\":{\"code\":\"ValidationFailed\",\"message\":\"Validation of indicator content failed.\",\"target\":\"body\",\"details\":[{\"code\":\"ValidationFailed\",\"message\":\"The value 1 is not a valid domain name.\",\"target\":\"domainName\"}]}};` 

Articles I have looked into for help:

  1. Python 2.7 Isolate multiple JSON objects in a string
  2. What is the correct JSON content type?

CodePudding user response:

First of all, you needed to get a message from error and then get the values with curly braces within the Message property.

Here is the simple line of code I have created to extract the error object.

const data = {
    "error": {
      "code": "",
      "message": "Http request failed with statusCode=BadRequest : {\"error\":{\"code\":\"ValidationFailed\",\"message\":\"Validation of indicator content failed.\",\"target\":\"body\",\"details\":[{\"code\":\"ValidationFailed\",\"message\":\"The value 1 is not a valid domain name.\",\"target\":\"domainName\"}]}}; ",
      "innerError": {
        "date": "2021-11-25T11:12:09",
        "request-id": "",
        "client-request-id": ""
      }
    }
  };

console.log(JSON.parse(data.error.message.match('({.*})')[1]));

This console.log will print results in this format:

{
  error: {
    code: 'ValidationFailed',
    message: 'Validation of indicator content failed.',
    target: 'body',
    details: [ [Object] ]
  }
}
  • Related