1. Stewart MacPherson
  2. PowerBuilder
  3. Friday, 2 December 2022 13:47 PM UTC

Hello,

We are developing in PowerBuilder 12.5 and I'm looking to find the best way to create a Json file format. Currently we are writing the data using a function out to a text file, Is there any way in PB 12.5 to create a json file with the format below? I can change the .txt to .json but it's not in the format below with the quotes etc.. Any help would be greatly appreciated. 

example how we want it to look

{"OriginJurisdiction":"YT","ControlNumber":"000202009","VersionNo":"000","SentCount":28339,"FileTime":"2020-09-04T00:05:39"}

our current coding 

ls_filename15 = "PE15" + ls_transmit_to + "recap.json"
ls_15_rec = "TransactionId" +":" + " " + ls_transtype + " " + "Vin" +":" + " " + ls_vin + " " + "Make" + ":" + ls_veh_make
//write the 15 record to the file
ls_full_filename = ls_path + ls_filename15
li_file15 = FileOpen(ls_full_filename, LineMode!, Write!, LockWrite!, Append!)
FileWrite(li_file15, ls_15_rec)
FileClose(li_file15)

Thanks

 

 
Armeen Mazda @Appeon Accepted Answer Pending Moderation
  1. Friday, 2 December 2022 16:21 PM UTC
  2. PowerBuilder
  3. # 1

You could create a COM wrapper for one of the popular JSON libraries so that you can utilize the third-party library in PB.  I believe some other people successfully integrated this library in PB 12.x: https://www.newtonsoft.com/json

 

Comment
  1. Stewart MacPherson
  2. Friday, 2 December 2022 17:15 PM UTC
Thanks for this info, I'll look into it.
  1. Helpful
There are no comments made yet.
mike S Accepted Answer Pending Moderation
  1. Friday, 2 December 2022 15:36 PM UTC
  2. PowerBuilder
  3. # 2

code it all manually.  12.x and below are well over 10 years old and SAP did very little (nothing) in terms of new features.  

 

If you want the new features such as json, you need to upgrade. 

Comment
  1. mike S
  2. Friday, 2 December 2022 15:37 PM UTC
also, you can bing/google json format and get a bunch of sites that allow you to verify your json
  1. Helpful 2
  1. Stewart MacPherson
  2. Friday, 2 December 2022 15:54 PM UTC
Thanks for the reply and yes I agree we need to upgrade but we are dealing with gov't and trying to get this in this file format in a time sensitive manner which won't allow us to upgrade and do a full system test.

When you say code it manually, can you expend on this ? One issue I had was getting quotes around the fields. If I use """ it thinks it's open ended quote error.



Thanks,

Stewart
  1. Helpful
There are no comments made yet.
Andreas Mykonios Accepted Answer Pending Moderation
  1. Friday, 2 December 2022 16:33 PM UTC
  2. PowerBuilder
  3. # 3

Take a look at Armeen's answer. What he says can be achieved in .net (a com visible object)...

Based on your conversation with Mike take a look at the following examples. They are both based on the information you provided. It's not the best solution but it can give you an idea of how to. Of course json can have more that one "rows" of data, and also nested information not to mention that you can even have arrays. So depending on how simplistic is your case it may be easy or complicated.

// 1st Example

string ls_15_rec, ls_transtype, ls_vin, ls_veh_make

ls_transtype = "My Transaction"
ls_vin = "My Vin"
ls_veh_make = "My value"

ls_15_rec = "{~n~"TransactionId~":~"" + ls_transtype + "~",~n~"Vin~":~"" + ls_vin + "~",~n~"Make~":~"" + ls_veh_make + "~"~n}"

messagebox("", ls_15_rec)

// 2nd Example

string ls_array1[], ls_array2[], ls_json = "", ls_open_char = "", ls_close_char = ""
char lc_type[] // C -> string, N -> number
long ll_i

ls_array1[upperbound(ls_array1) + 1] = "OriginJurisdiction"
ls_array2[upperbound(ls_array1)] = "YT"
lc_type[upperbound(ls_array1)] = "C"
ls_array1[upperbound(ls_array1) + 1] = "ControlNumber"
ls_array2[upperbound(ls_array1)] = "000202009"
lc_type[upperbound(ls_array1)] = "C"
ls_array1[upperbound(ls_array1) + 1] = "VersionNo"
ls_array2[upperbound(ls_array1)] = "000"
lc_type[upperbound(ls_array1)] = "C"
ls_array1[upperbound(ls_array1) + 1] = "SentCount"
ls_array2[upperbound(ls_array1)] = string(28339)
lc_type[upperbound(ls_array1)] = "N"
ls_array1[upperbound(ls_array1) + 1] = "FileTime"
ls_array2[upperbound(ls_array1)] = "2020-09-04T00:05:39"
lc_type[upperbound(ls_array1)] = "C"

for ll_i = 1 to upperbound(ls_array1)
	if lc_type[ll_i] = 'N' then
		ls_open_char = ""
		ls_close_char = ""
	else
		ls_open_char = "~""
		ls_close_char = "~""
	end if
	if ll_i < upperbound(ls_array1) then
		ls_close_char = ls_close_char + ",~n"
	else
		ls_close_char = ls_close_char + "~n"
	end if
	ls_json = ls_json + "~"" + ls_array1[ll_i] + "~":~"" + ls_open_char + ls_array2[ll_i] + ls_close_char
next

ls_json = "{~n" + ls_json + "~}"

messagebox("", ls_json)

Andreas.

Comment
  1. Stewart MacPherson
  2. Friday, 2 December 2022 17:16 PM UTC
Thanks for this idea, I will take a look into this suggestion
  1. Helpful
  1. Stewart MacPherson
  2. Friday, 2 December 2022 17:41 PM UTC
Thanks, we believe sample 1 will work. we are just verifying.. Much appreciated
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


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