1

I have some data i am pulling from a web service. This is the string

(Body:'3886' MessageProperties [headers={}, timestamp=null, messageId=null, userId=null, receivedUserId=null, appId=null, clusterId=null, type=null, correlationId=null, correlationIdString=null, replyTo=null, contentType=application/x-java-serialized-object, contentEncoding=null, contentLength=0, deliveryMode=null, receivedDeliveryMode=PERSISTENT, expiration=null, priority=0, redelivered=false, receivedExchange=, receivedRoutingKey=bottomlesspit, receivedDelay=null, deliveryTag=62, messageCount=0, consumerTag=amq.ctag-sCwfLaMEqWp2GkFwFrY1yg, consumerQueue=bottomlesspit])

It looks like json but the key value pairs are almost fine but the most important key which is Body isn't like other keys as the string would tell.

I need to read the value of Body and be able to get the value like this

   console.log(d.body);
   //This above outputs the string as shown
   obj = eval('{' + d.body + '}');
   console.log(obj);
   var match = "Body";
   var val = obj.find( function(item) { return item.key == match } );
   console.log(val);

How can i read the value of the key Body?.

3
  • For just Body, would be much easier (and safer) to use a regular expression instead Commented May 21, 2018 at 9:36
  • This is neither JSON nor valid javascript, you can't eval it. You need to implement a parser or old good regex as suggested above. Commented May 21, 2018 at 9:38
  • Ok thanks, i will just have to regex it. Commented May 21, 2018 at 9:41

3 Answers 3

1

Use this regular expression instead of a match Body:

\bBody:'(\d*)'

This will catch the Body number in group 1.

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

2 Comments

I have this var str = d.body var res = str.match(\bBody:'(\d*)'); where str is the string but gives a syntax error
@Gandalf is the ' seen as special caracter or changing the code?
1

You can write a parser function get string and extract values. A very simple function is here. You can modify it also for all exceptions exist.

var str = `(Body:'3886' MessageProperties [headers={}, timestamp=null, messageId=null, userId=null, receivedUserId=null, appId=null, clusterId=null, type=null, correlationId=null, correlationIdString=null, replyTo=null, contentType=application/x-java-serialized-object, contentEncoding=null, contentLength=0, deliveryMode=null, receivedDeliveryMode=PERSISTENT, expiration=null, priority=0, redelivered=false, receivedExchange=, receivedRoutingKey=bottomlesspit, receivedDelay=null, deliveryTag=62, messageCount=0, consumerTag=amq.ctag-sCwfLaMEqWp2GkFwFrY1yg, consumerQueue=bottomlesspit])`;

function f(inp) {
  var index = str.indexOf(inp),
       endIndex;
  for(var i = index; i < str.length; i ++) {
    if(str[i] == ',') {
      endIndex = i;
      break;
    }
  }
  var output = str.substr(index, endIndex).split('=');
  return output;
}

console.log(f('consumerQueue'));

Comments

1

Why not use a regex to match and extract the Body.

Example:

const match = d.body.match(/Body:\'(.+)\'/)
if (match) {
  const body = match[1] // This is the value of Body
} else {
   // Unable to find Body, handle it here
}

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.