0

Check if output in the Object of arrays (steps) has 'error' and display the message in the 'error' along with the particular id.

I want to display the error if exists in JavaScript.

Here is the JSON,

{
  "steps": [
    {
      "start": null,
      "stop": null,
      "status": "PENDING",
      "input": null,
      "output": null,
      "id": "45968631-4b24-4b80-a618-954ec383ce8d"
    },
    {
      "start": "2019-08-23T00:11:27.323325Z",
      "stop": "2019-08-23T00:11:50.581740Z",
      "status": "SUCCESS",
      "input": {
        "url": "https://www.google.com"
      },
      "output": {
        "filepath": "/tmp/filepath"
      },
      "id": "153eec8e-aff0-4566-9dee-bd2235f59886"
    },
    {
      "start": "2019-08-23T00:26:31.377313Z",
      "stop": "2019-08-23T00:26:58.489024Z",
      "status": "SUCCESS",
      "input": null,
      "output": {
        "url": "url"
      },
      "id": "cb2280a8-3b75-4e7b-9052-42a563b4fd9e"
    },
    {
      "start": "2019-08-23T00:41:00.988154Z",
      "stop": "2019-08-23T00:41:04.528278Z",
      "status": "SUCCESS",
      "input": {
        "key": "userKey"
      },
      "output": {
        "error": "ProcessorError"
      },
      "id": "65324ed2-d347-4a35-8fdc-fe11b98d5e70"
    }
  ]
}

There are some places, where Output does not have 'error' and some it has null.

3
  • 1
    What research have you done or what code have you written to achieve this? It's expected that you have attempted to answer your own question before asking us, otherwise your question is nothing more than a code request. Please edit your question to include your attempts at completing your own request so that we can help you debug. Commented Aug 28, 2019 at 19:20
  • 1
    Possible duplicate of Search recursively for value in object by property name Commented Aug 28, 2019 at 19:26
  • @TylerRoper , for your question, I had initially thought of this and It was not matching my expectations for output, Yes may be I need to learn more of filter and map. Here is what I tried. checkError(data){ console.log('ERROR', JSON.stringify(data, null, 2)); const vals = data.steps.filter( (ele) => { const message = ''; if(ele.output !== null) { this.message = ele.output.error; console.log('Message', message); } return message; }); Commented Aug 29, 2019 at 15:28

3 Answers 3

1

You can simply achieve this with filter() and map() method.

const obj = {
    "steps": [{
            "start": null,
            "stop": null,
            "status": "PENDING",
            "input": null,
            "output": null,
            "id": "45968631-4b24-4b80-a618-954ec383ce8d"
    }, {
            "start": "2019-08-23T00:11:27.323325Z",
            "stop": "2019-08-23T00:11:50.581740Z",
            "status": "SUCCESS",
            "input": {
                    "url": "https://www.google.com"
            },
            "output": {
                    "filepath": "/tmp/filepath"
            },
            "id": "153eec8e-aff0-4566-9dee-bd2235f59886"
    }, {
            "start": "2019-08-23T00:26:31.377313Z",
            "stop": "2019-08-23T00:26:58.489024Z",
            "status": "SUCCESS",
            "input": null,
            "output": {
                    "url": "url"
            },
            "id": "cb2280a8-3b75-4e7b-9052-42a563b4fd9e"
    }, {
            "start": "2019-08-23T00:41:00.988154Z",
            "stop": "2019-08-23T00:41:04.528278Z",
            "status": "SUCCESS",
            "input": {
                    "key": "userKey"
            },
            "output": {
                    "error": "ProcessorError"
            },
            "id": "65324ed2-d347-4a35-8fdc-fe11b98d5e70"
    }]
}

const result = obj.steps.
filter((r) => {
    return r.output && r.output.error;
}).map((m) => {
    return {
            id: m.id,
            error: m.output.error
    }
});
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

1

Not exactly sure what you want to do here but this is how you could check for the error in each one and do something accordingly.

let data={"steps":[{"start":null,"stop":null,"status":"PENDING","input":null,"output":null,"id":"45968631-4b24-4b80-a618-954ec383ce8d"},{"start":"2019-08-23T00:11:27.323325Z","stop":"2019-08-23T00:11:50.581740Z","status":"SUCCESS","input":{"url":"https://www.google.com"},"output":{"filepath":"/tmp/filepath"},"id":"153eec8e-aff0-4566-9dee-bd2235f59886"},{"start":"2019-08-23T00:26:31.377313Z","stop":"2019-08-23T00:26:58.489024Z","status":"SUCCESS","input":null,"output":{"url":"url"},"id":"cb2280a8-3b75-4e7b-9052-42a563b4fd9e"},{"start":"2019-08-23T00:41:00.988154Z","stop":"2019-08-23T00:41:04.528278Z","status":"SUCCESS","input":{"key":"userKey"},"output":{"error":"ProcessorError"},"id":"65324ed2-d347-4a35-8fdc-fe11b98d5e70"}]}

data.steps.forEach(item => {
  //  Check for output's existence then an error's existence
  if (!!item.output && !!item.output.error) {
    console.log(item.output.error);
  }
});

2 Comments

if "output.error" is an empty string, this code would say it doesn't exist. Better to use if ("output" in item && "error" in item.output).
@HereticMonkey The prompt said output could be null or error could not exist on the object. I went off that and solved those cases. Though yes, it does not account for empty strings.
1

save your data in a variable var data and apply this sentence

data.steps.filter(x=>x.output && x.output.error).map(result=>({id:result.id,errro:result.output.error}))

1 Comment

if "output.error" is an empty string, this code would say it doesn't exist. Better to use "output" in x && "error" in x.output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.