Need Urgent Help for output STring

Posted by Krishna Kumar on 31-May-2016 05:59

I'm running below query and taking the values in the variable -

FOR EACH ttTableRecords BREAK BY ttTableRecords.field_recid :

   ASSIGN lcFieldValue = lcFieldValue + ttTableRecords.field_value.
    ASSIGN lcFieldValue =   lcFieldValue  + ",".

END.

However output is coming in below format -

lcfieldValue = "20yr,20 Year Membership,MARCUS,MARCUS,HO,DVLP,,"

Thanks,

Krishna Kumar

I want output should come in below format -

lcfieldValue = "20yr","20 Year Membership","MARCUS","MARCUS","HO", "DVLP"

All Replies

Posted by cverbiest on 31-May-2016 06:05

You're only adding comma's to the output. If you want quotes you have to add them

ASSIGN lcFieldValue = lcFieldValue + '"' + ttTableRecords.field_value + '"'.

Posted by Mark Davies on 31-May-2016 06:08

You could also just change this line:

ASSIGN lcFieldValue = lcFieldValue + ttTableRecords.field_value.

To this:

ASSIGN lcFieldValue = lcFieldValue + QUOTER(ttTableRecords.field_value).

Posted by Krishna Kumar on 31-May-2016 06:22

I TRIED. however OUTPUT IS COMING LIKE THIS - Why "/" is coming in output  -

"\"20yr\",\"20 Year Membership\",\"MARCUS\",\"MARCUS\",\"HO\",\"DVLP\",\"\",\"\""

Posted by Krishna Kumar on 31-May-2016 06:25

I tried. However output is coming in below format . Please let me know why "/" is coming

"\"20yr\",\"20 Year Membership\",\"MARCUS\",\"MARCUS\",\"HO\",\"DVLP\",\"\",\"\""

Thanks,

Krishna

Posted by Mark Davies on 31-May-2016 06:37

It looks like it is being escaped - that is usually what the \ is for.

How are you writing your output?

Is lcFieldValue a LONGCHAR?

Any reason why you didn't opt for using EXPORT DELIMITER "," ttTableRecords. ? That will help if you need to go directly to file and will dump quotes around string values.

Posted by bronco on 31-May-2016 06:38

Well, you re using the quoter function. The \" is the escape code for double quotes with a double quoted string.

Posted by Krishna Kumar on 31-May-2016 06:49

Hi Mark,

Here is the exact problem.

We are writing a rest API and storing the data in temp-table. Then we are using PDS-OE to map and output the same in JSON Format. Below is the JSON output which we are getting after  using -

ASSIGN lcFieldValue = lcFieldValue + QUOTER(ttTableRecords.field_value).

JSON FILE Output

-------------------------

{

 "response": {

   "data": {

     "data": [

       {

         "Table_recid": 27137,

         "columnData": "\"20yr\",\"20 Year Membership\",\"MARCUS\",\"MARCUS\",\"HO\",\"DVLP\",\"\",\"\""

       },

       {

         "Table_recid": 27138,

         "columnData": "\"ANFH\",\"\",\"\",\"\",\"\",\"\",\"\",\"Penthouse\""

       },

       {

         "Table_recid": 27139,

         "columnData": "\"Anfi preffered\",\"ANFP\",\"\",\"\",\"\",\"\",\"\",\"\""

       },

       {

         "Table_recid": 27140,

         "columnData": "\"\",\"\",\"\",\"\",\"Anfi Regular\",\"ANFR\",\"\",\"\""

       },

       {

Thanks,

Krishna Kumar

Posted by bronco on 31-May-2016 06:57

Don't know why quoter was suggested, just remove it.

ASSIGN lcFieldValue = lcFieldValue + "," + ttTableRecords.field_value.

at the end:

assign lcFieldValue = trim(lcFieldValue, ",").

to get rid of the leading comma.

Posted by Krishna Kumar on 31-May-2016 07:11

Hi Bronco,

I tried with your approach. However output is coming like this -

"response": {

   "data": {

     "data": [

       {

         "Table_recid": 27137,

         "columnData": "20yr,,20 Year Membership,,MARCUS,,MARCUS,,HO,,DVLP"

       },

       {

         "Table_recid": 27138,

         "columnData": "ANFH,,Penthouse"

       },

       {

         "Table_recid": 27139,

         "columnData": "Anfi preffered,,ANFP"

       },

       {

         "Table_recid": 27140,

         "columnData": "Anfi Regular,,ANFR"

       },

       {

I want it to come in below format -

 " "20yr", "20 Year Membership" , "MARCUS", "MARCUS","HO", "DVLP"

Posted by bronco on 31-May-2016 07:27

then the quoter function is OK and so is the output of

"\"20yr\",\"20 Year Membership\",\"MARCUS\",\"MARCUS\",\"HO\",\"DVLP\",\"\",\"\""

because that's how it works when you want to have double quotes within a JSON string.

What you want (last post) is not possible since that would produce invalid JSON.

Posted by Mark Davies on 31-May-2016 07:28

Aha - well, that changes your approach then...

Since JSON is seeing this as a string you will not be able to have the values inside the value as quoted strings - unless you escape them - which what QUOTER did. You will not be able to do that any other way if you want a double quote. The alternative is to rather use a single quote instead "'20y','','20 Year Membership',..."

Not the best way to represent data I have to admit - a child object would have been a better approach in JSON.

Posted by bronco on 31-May-2016 07:32

or an array of strings

Posted by Krishna Kumar on 31-May-2016 07:36

Hi Bronco,

Thanks for your suggestions. However we want JSON in below form-

"data":[

     {

       "recId":"0001",

       "columnData":[

         "a0","b0","c0","d0","e0","a0","b0","c0","d0","e0","v","f"

       ]

     }

Is there any way for the same?

Thanks,

Krishna Kumar

Posted by Brian K. Maher on 31-May-2016 07:40

Krishna,

May I be so bold as to suggest that what you are seeing in the JSON results is actually what you want since it is valid JSON and that whatever JSON parser you are (or should be) using to parse this JSON will properly handle the escaped double quotes and return to you the data in the form you want.

Brian

Posted by Mike Fechner on 31-May-2016 07:40

That would be a JSON String Array - as Bronco suggested. Instead of Attempting to build (complex) JSON manually, try using the JSON Object Model Parser from the ABL. Progress.Json.ObjectModel.* classes.

Posted by Brian K. Maher on 06-Jun-2016 11:50

You don’t say what version your are running, however, look and see if the QUOTER function is available to you and if it is, use it.

Posted by Brian K. Maher on 06-Jun-2016 11:50

FOR EACH ttTableRecords BREAK BY ttTableRecords.field_recid :

   ASSIGN lcFieldValue = lcFieldValue + QUOTER(ttTableRecords.field_value).
    ASSIGN lcFieldValue =   lcFieldValue  + ",".

END.

 

Posted by Brian K. Maher on 06-Jun-2016 11:50

Krishna,
 
First, we need to know the version you are running and the operating system you are running it on.
 
Second, send us the code which produced that output.
 
Brian

Posted by Brian K. Maher on 06-Jun-2016 11:50

Krishna,
 
I would think that this is expected behavior.  The data in the string is having all double quotes escaped with the backslash because the system needs to be able to ignore them so that the enclosing double quotes are what are processed.
 
Brian

Posted by Brian K. Maher on 06-Jun-2016 11:50

It was suggested because he said he wanted each field to be wrapped within double quotes (i.e. “abc”,”def”,”123”).
 

Posted by Brian K. Maher on 06-Jun-2016 11:50

Actually, QUOTER did not escape them.  The conversion to JSON did.  :-)

This thread is closed