2

I'm receiving this error response from an api:

[HTTP 400] - BAD_REQUEST 0 [53015] - sender name invalid value: almir1 [53044] - credit card holder name invalid value: almir

How could i transform this response to an object or array in javascript?

for example:

     var errors = [
        {key : 53015: message: "sender name invalid value"},
        {key : 53044: message: "credit card holder name invalid value: almir"}
    ];
4
  • Your example doesn't show up, can you try again? Commented Apr 10, 2018 at 19:03
  • Sorry, i fixed it. Commented Apr 10, 2018 at 19:07
  • Looks like simple String processing problem Commented Apr 10, 2018 at 19:08
  • i'm sorry guys, i fixed it again. Commented Apr 10, 2018 at 19:18

3 Answers 3

2

This should do the trick:

const text = `[HTTP 400] - BAD_REQUEST 0 [53015] - sender name invalid 
value: almir1 [53044] - credit card holder name invalid value: almir`;
let regex = /\[([^\]]+?)\]\s\-\s([^\[]+)/g;
let matches;
const errors = {};
while (matches = regex.exec(text)) {
    errors[matches[1]] = matches[2];
}

Output:

{
    "53015": "sender name invalid value: almir1 ",
    "53044": "credit card holder name invalid value: almir",
    "HTTP 400": "BAD_REQUEST 0 "
}

To create a key/message array use this instead

const text = `[HTTP 400] - BAD_REQUEST 0 [53015] - sender name invalid value: almir1 [53044] - credit card holder name invalid value: almir`;
let regex = /\[([^\]]+?)\]\s\-\s([^\[]+)/g;
let matches;
const errors = [];
while (matches = regex.exec(text)) {
    errors.push({
        key: matches[1],
        message: matches[2]
    });
}

Output:

[
    {
        "key": "HTTP 400",
        "message": "BAD_REQUEST 0 "
    },
    {
        "key": "53015",
        "message": "sender name invalid value: almir1 "
    },
    {
        "key": "53044",
        "message": "credit card holder name invalid value: almir"
    }
]
Sign up to request clarification or add additional context in comments.

5 Comments

That still doesn't create an object.
@Paul It prints the last match, when executed in browser's console. Try to console.log(errors)
I did, it prints an array rather than an object.
@Paul: it works fine for me. Perhaps you should check if you copied the code correctly?
It was literally a copy and paste, I typed nothing. Note, however, the code has changed since I made my comment.
0

You can use: var aRes = str.split('['); to get an array of strings split by '[' and then further process these aRes strings to the desired json "error" object.

Comments

0

To make an array, you can match on the pattern of each error:

var regex = /\[\d+\]\s\-\s([^\[+]+)/gi
errorMessage.match(regex);

// returns 
// [ '[53015] - sender name invalid value: almir1 ',
//  '[53044] - credit card holder name invalid value: almir' ]

If you want to convert that into an object, you can use capture groups:

var regex = /\[(\d+)\]\s\-\s([^\[]+)/gi
var errors = {};
var error;
while (error = regex.exec(errorMessage)) {
  errors[error[1]] = error[2];
}

Comments

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.