1. SHIJILESH CHERROT
  2. PowerBuilder
  3. Saturday, 07 April 2018
Hi,
 
Here is my code for generating JSON file from datawindow. it shows some issues.
The out put should show like below 
[
{
    "postId": 1,
    "id": 1,
    "name": "id labore ex et quam laborum",
    "email": "Eliseo@gardner.biz",
    "body": "laudantium enim "
  },
  {
    "postId": 1,
    "id": 2,
    "name": "quo vero reiciendis velit similique earum",
    "email": "Jayne_Kuhic@sydney.com",
    "body": "est natus enim nihil "
  },
  ]

but i get result like this


  {
    "postId": 1,
    "id": 1,
    "name": "id labore ex et quam laborum",
    "email": "Eliseo@gardner.biz",
    "body": "laudantium enim quasi est"
   ,
    "postId": 1,
    "id": 2,
    "name": "quo vero reiciendis velit similique earum",
    "email": "Jayne_Kuhic@sydney.com",
    "body": "est natus enim nihil est"
  },

 

//////////////////////////////// Function for generating JSON ////////////////////////////////////////////////////////////////
 
long    ll_index, ll_count,  ll_root  
String      ls_json
long columns ,ll_col_index , ll_col_count,ln_long ,ll_RootArray 
string ls_colname,ls_datastring,coltype,ls_string
dec ld_dec
datetime ld_dattime
int lint
 
HttpClient     hc  
JSONGenerator jg   
 
hc = create HttpClient  
jg = create JSONGenerator   
 
this.AcceptText()  
 
ll_count = this.Rowcount( )  
  
ll_col_count = integer(this.object.datawindow.column.count)
  
ll_root = jg.createjsonobject( )  
 
 FOR ll_index = 1 TO ll_count  
        
// ll_root =jg.CreateJsonArray() 
  
coltype =UPPER(LEFT( this.Describe(ls_datastring),3))
 
for ll_col_index  = 1 To ll_col_count
 
ls_colname = this.describe('#'+string(ll_col_index)+'.name')
ls_datastring = ls_colname+"."+"Coltype"
coltype =UPPER(LEFT( this.Describe(ls_datastring),3))
 
  if coltype = "CHA" then
ls_string = this.GetItemString(ll_index, ls_colname  )
if isnull(ls_string) then 
jg.AddItemNull (ll_root, ls_colname  )
else
jg.AddItemString(ll_root, ls_colname ,ls_string)  
end if
elseif coltype = "DAT" then   
ld_dattime= this.GetItemDateTime (ll_index, ls_colname  )
if isnull(ld_dattime) then  
jg.AddItemNull (ll_root, ls_colname  )  
else
jg.AddItemDateTime(ll_root, ls_colname , ld_dattime )  
end if 
elseif coltype = "DEC" then 
ld_dec =this.GetItemNumber(ll_index, ls_colname) 
if isnull(ld_dec) then  
jg.AddItemNull (ll_root, ls_colname  )  
else 
jg.AddItemNumber(ll_root,  ls_colname , ld_dec)
end if 
elseif coltype = "LON" then 
ln_long =this.GetItemNumber(ll_index, ls_colname) 
if isnull(ln_long) then  
jg.AddItemNull (ll_root, ls_colname  )  
else 
jg.AddItemNumber(ll_root,  ls_colname , ln_long)
end if 
elseif coltype = "INT" then  
lint =this.GetItemNumber(ll_index, ls_colname) 
if isnull(ln_long) then  
jg.AddItemNull (ll_root, ls_colname  )  
else 
jg.AddItemNumber(ll_root,  ls_colname , lint)
end if 
else
messagebox('info','Data type undefined')
end if
Next
 
               
 NEXT  
 
ls_json = jg.getjsonstring( )
 
messagebox('', ls_json ) 
 
Thanks
Shijilesh
Accepted Answer
mike S Accepted Answer Pending Moderation
0
Votes
Undo

you need to create an array of objects.  you created just an object. 

start with createjsonarray, then in your ROW loop create your object as an array value and then in your column loop add your items to the object.  

ll_Aroot =jg.CreateJsonArray() 
 
 FOR ll_index = 1 TO ll_count  
        ll_root =jg.AddItemObject(ll_Aroot)   
       for ll_col = 1 to colcount
           jg.Additem( ll_root, dw.getitem())
​     next
next
 
Comment
Thanks It worked :)

  1. SHIJILESH CHERROT
  2. Sunday, 8 April 2018
There are no comments made yet.
  1. Saturday, 7 April 2018
  2. PowerBuilder
  3. # Permalink
Armeen Mazda Accepted Answer Pending Moderation
0
Votes
Undo

Please open a support ticket if you suspect something is a bug of Appeon products: https://www.appeon.com/standardsupport/

Comment
Sorry it was my mistake 

  1. SHIJILESH CHERROT
  2. Sunday, 8 April 2018
There are no comments made yet.
  1. Saturday, 7 April 2018
  2. PowerBuilder
  3. # 1
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.