ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum IndexWebSphere Message Broker SupportFormatting/indenting JSON messages

Post new topicReply to topic
Formatting/indenting JSON messages View previous topic :: View next topic
Author Message
rekarm01
PostPosted: Mon Apr 03, 2017 2:48 pm Post subject: Formatting/indenting JSON messages Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 1232

I have a message flow that writes a JSON message to a queue using the MQOutput node, and also writes it to an email using the EMailOutput node:

Code:
-- write to queue
SET OutputRoot.Properties.CodedCharSetId = 1208; -- utf-8
SET OutputRoot.MQMD.Format = MQFMT_STRING;
SET OutputRoot.JSON = InputRoot.JSON;
PROPAGATE TO TERMINAL 'out';

-- write to email
SET OutputRoot.Properties.CodedCharSetId = 1208; -- utf-8
SET OutputRoot.EmailOutputHeader.To = InputLocalEnvironment.Variables.EmailTo;
SET OutputRoot.EmailOutputHeader.Subject = ' ... Subject ... ';
SET OutputRoot.BLOB.BLOB = ASBITSTREAM(InputRoot.JSON OPTIONS EmbeddedBitStream);
PROPAGATE TO TERMINAL 'out1';

-- don't propagate again
RETURN FALSE;


The message put to queue is formatted nicely, one element per line, and indented to show the structure of the message. The message posted to the email, however, is not nicely formatted, or indented; it's just one long line of raw JSON text. I would not have expected the JSON parser to format/indent in either case, but if I had to choose one, I would prefer it for the human-readable email, rather than the queue.

Is there some parser option that I can specify to control the formatting/indenting of JSON messages on output?
Back to top
View user's profile Send private message
mpong
PostPosted: Mon Apr 03, 2017 3:07 pm Post subject: Reply with quote

Centurion

Joined: 22 Jan 2010
Posts: 108

Are you trying to send entire input message as en email body? Email body typically consists of the information about something not the input message.

Why don't you attach it as it .json file, you can use email attachment content properties too.
Back to top
View user's profile Send private message
mqjeff
PostPosted: Tue Apr 04, 2017 4:47 am Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17174

mpong wrote:
Email body typically consists of the information about something not the input message.


An email body can contain any kind of data. I once worked with a system that sent the (encrypted) contents of business data as the body of an email message.

Using a MIME document may be a better choice, to provide more clarity about what the data actually is, and how to ensure that it has been transmitted properly...

But the receiving application may not be able to support that.
_________________
Read, Think, Try, Repeat
Back to top
View user's profile Send private message
rekarm01
PostPosted: Tue Apr 04, 2017 8:30 am Post subject: Re: Formatting/indenting JSON messages Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 1232

mpong wrote:
Why don't you attach it as it .json file ... ?

How exactly does that help with the question at hand?

Something seems to be causing the JSON parser to indent/format messages when put to a queue. Is that something I can use when also writing the same message to an email? And if so, then how?
Back to top
View user's profile Send private message
mqjeff
PostPosted: Tue Apr 04, 2017 8:57 am Post subject: Re: Formatting/indenting JSON messages Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17174

rekarm01 wrote:
mpong wrote:
Why don't you attach it as it .json file ... ?

How exactly does that help with the question at hand?

Something seems to be causing the JSON parser to indent/format messages when put to a queue. Is that something I can use when also writing the same message to an email? And if so, then how?

JSON shouldn't care about intermediate spacing...

Are you, somehow, trying to make the json file readable in the email message?

That seems a bit odd...

If you want the ASBITSTREAM to behave as it would with an output node then don't use EmbeddedBitStream, use RootBitstream..
_________________
Read, Think, Try, Repeat
Back to top
View user's profile Send private message
adubya
PostPosted: Tue Apr 04, 2017 9:43 am Post subject: Re: Formatting/indenting JSON messages Reply with quote

Partisan

Joined: 25 Aug 2011
Posts: 365
Location: GU12, UK

rekarm01 wrote:

The message put to queue is formatted nicely, one element per line, and indented to show the structure of the message.


Are you sure about that formatting ? It's not a feature of whatever data viewer you're using ? Or have you verified that the MQ message contains indenting spaces/tabs etc ?

I'd not expect IIB to perform pretty-print formatting, its generating machine readable data. I wouldn't want superfluous intentation characters being generated, that's what a decent data viewer does.
_________________
Independent Middleware Consultant
andy@knownentity.com
Back to top
View user's profile Send private message Send e-mail
timber
PostPosted: Tue Apr 04, 2017 2:53 pm Post subject: Reply with quote

Yatiri

Joined: 25 Aug 2015
Posts: 614

I agree with mqjeff. I would be very surprised if the JSON parser knows or cares what type of transport the message is going to be written to. It's just creating a character stream.
I would first look at the raw bytes of the message to decide whether the viewer is making MQ's output look good.
Back to top
View user's profile Send private message
rekarm01
PostPosted: Tue Apr 04, 2017 6:40 pm Post subject: Re: Formatting/indenting JSON messages Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 1232

mqjeff wrote:
Are you, somehow, trying to make the json file readable in the email message?

That seems a bit odd...

Which part of that seems odd? Putting a JSON message in an email, or wanting to make the email more readable?

The requirements for the email content are still a work in progress, but for now, it includes a JSON message.

adubya wrote:
Are you sure about that formatting? It's not a feature of whatever data viewer you're using? Or have you verified that the MQ message contains indenting spaces/tabs etc.?

Yes, I'm sure that my slightly modified amqsgbr0 and amqsbcg0 sample programs are not pretty-printing my JSON messages.

adubya wrote:
I wouldn't want superfluous intentation characters being generated, that's what a decent data viewer does.

No, I wouldn't really want that either, for a machine readable queue, but I might want it for a human readable email. My bloated email reader doesn't always qualify as a "decent data viewer", and switching back and forth between applications just to read an email can get inconvenient.

timber wrote:
I would be very surprised if the JSON parser knows or cares what type of transport the message is going to be written to.

Upon closer examination, the message flow calls a RESTful service, and it's the RESTful service that does the formatting/indenting when it generates a JSON message and returns it to the message flow.

So, the more likely explanation is that the message flow is not parsing the message at all, when it writes to a queue; it's just passing along the unparsed input bitstream as-is, added white space and all. But the message flow is fully parsing and reserializing the message, (unnecessarily), when it writes to an email, stripping out any excess white space in the process.

If that's the case, then how might I get the message flow to pass along the unparsed input bitstream as-is, when writing to an email, rather than parsing it, and stripping out the excess white space?
Back to top
View user's profile Send private message
adubya
PostPosted: Tue Apr 04, 2017 11:18 pm Post subject: Reply with quote

Partisan

Joined: 25 Aug 2011
Posts: 365
Location: GU12, UK

Are you using a HTTPRequest node to invoke the RESTful service ?

If so, what's the domain set to in the Response Message Parsing property set to ? I'm assuming JSON based upon your code snippets.

If you set it to BLOB and handle the response as a BLOB throughout your flow to the point you create your EMail (or store the BLOB representation somewhere in the environment) then the pretty print formatting should be preserved.
_________________
Independent Middleware Consultant
andy@knownentity.com
Back to top
View user's profile Send private message Send e-mail
mqjeff
PostPosted: Wed Apr 05, 2017 3:49 am Post subject: Re: Formatting/indenting JSON messages Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17174

[quote="rekarm01"]
mqjeff wrote:
Are you, somehow, trying to make the json file readable in the email message?

That seems a bit odd...

rekarm01 wrote:
Which part of that seems odd? Putting a JSON message in an email, or wanting to make the email more readable?


Trying to make it more readable... JSON is like XML. It's intended to carry data in an easily (well, so not like XML) parsed format. It's not intended to be human readable.

rekarm01 wrote:
So, the more likely explanation is that the message flow is not parsing the message at all, when it writes to a queue; it's just passing along the unparsed input bitstream as-is, added white space and all. But the message flow is fully parsing and reserializing the message, (unnecessarily), when it writes to an email, stripping out any excess white space in the process.

So you don't have access to the message flow itself? Or why is it "more likely"?

I agree about the parser. I don't personally know of any way to control how the parser outputs messages like this. There are various ways to prety-print JSON, you could serialize it, reparse it as a JSON message in Java, and then serialize it in Java again...

But I still find it odd that it would be useful to make the JSON contents of an email more human-readable...

rekarm01 wrote:
If that's the case, then how might I get the message flow to pass along the unparsed input bitstream as-is, when writing to an email, rather than parsing it, and stripping out the excess white space?


Don't parse it? Build a straight forward MIME tree, with a single part, and attach the BLOB as the message body... Or maybe you don't even need to create a MIME tree, just set the BLOB as the email body?
_________________
Read, Think, Try, Repeat
Back to top
View user's profile Send private message
rekarm01
PostPosted: Thu Apr 06, 2017 12:12 pm Post subject: Re: Formatting/indenting JSON messages Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 1232

mqjeff wrote:
So you don't have access to the message flow itself? Or why is it "more likely"?

Yes, I have access to the message flow. I was trying to find an explanation for the difference in the observed output when writing to queue versus when writing to email. It was "more likely" that the message flow preserved existing white space in the message when writing to queue, by not invoking the parser, rather than that the parser had an option to add white space, as I had first asked about.

mqjeff wrote:
There are various ways to prety-print JSON, you could serialize it, reparse it as a JSON message in Java, and then serialize it in Java again...

I would settle for preserving existing white space, as the message flow currently does when writing a message to queue, rather than discarding it, as it currently does when writing a message to email.

mqjeff wrote:
Don't parse it? Build a straight forward MIME tree, with a single part, and attach the BLOB as the message body... Or maybe you don't even need to create a MIME tree, just set the BLOB as the email body?

Please refer to the ESQL in my initial post. Just setting the BLOB as the email body is what causes the parsing to occur. The question was how to prevent that. mpong had also suggested a MIME attachment, but that would make the email even harder to read, and it still doesn't stop the parser from discarding existing white space.

adubya wrote:
If you set [the HTTPRequest node's Message Domain property] to BLOB, ... then the pretty print formatting should be preserved.

Hmm, that could work. It's worth a try ...
Back to top
View user's profile Send private message
mqjeff
PostPosted: Fri Apr 07, 2017 6:32 am Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17174

An email message with a single MIME part should be pretty indistinguishable from an email message that's not a MIME document at all...

You might also try using RootBitStream instead of EmbeddedBitStream... as I said earlier, that makes the parser work like an output node - so you should get the same message string as you get on the queue, instead of the one you're getting in your email.
_________________
Read, Think, Try, Repeat
Back to top
View user's profile Send private message
Display posts from previous:
Post new topicReply to topic Page 1 of 1

MQSeries.net Forum IndexWebSphere Message Broker SupportFormatting/indenting JSON messages
Jump to:



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP


Theme by Dustin Baccetti
Powered by phpBB 2001, 2002 phpBB Group

Copyright MQSeries.net. All rights reserved.