Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Array to CSV / XML help needed #4

Closed
prashantnirgun opened this issue Sep 23, 2022 · 2 comments
Closed

JSON Array to CSV / XML help needed #4

prashantnirgun opened this issue Sep 23, 2022 · 2 comments

Comments

@prashantnirgun
Copy link

prashantnirgun commented Sep 23, 2022

I am using PB 12.5 Classical and I want to display json data into datawindow. PB REST is supported from 2019 onwards I believe. my json response is a standard array with fixed structure, my idea is to picked up only values and put it in a string and append , end of row.

01 Do we have any function to determine which will extract only values something like Object.toString().
02 If I parse it one row at a time how do I find how many keys in the row ?
03 XML generator
[
{ "col1":"r1c1","col2":"r1c2"},
{ "col1":"r2c1","col2":"r2c2"},
]
Thanks in advance

@xlat
Copy link
Owner

xlat commented Sep 23, 2022

Hi,
01: not sure what you mean, is the method tojson() what you need?
02: you may use getattributes ( ref string as_names[] ) returns long to get list of attributes (the object "keys")
03: you have to write a visitor to build the xml yourself, there is not automatic way available at know, may be a feature request?

@prashantnirgun
Copy link
Author

Hey @xlat thanks resolved however my code is as follows just check once if there is any improvement let me know, My requirement was to only display the report and not edit or update so this is ok for me also my array size is also limited.

int li_start, li_end, li_col_start, li_col_count
String ls_error, ls_response, ls_value, ls_attributes[], ls_key, ls_row
any la_data, la_contents[]
json ln_json, ln_item

ln_json = create json
ls_response = '{"contents": [{"id" : "1", "name" : "one"},{ "id" : "2", "name" : "Two"}]}'
ls_error = ln_json.parse(ls_response)

if ls_error = "" then
	ln_json.retrieve("contents", ref la_data)
	la_contents[] = la_data
	li_end = upperbound( la_contents[] )
	//MessageBox("Info", "Name : " + string(ln_item.getattribute("name")))
	For li_start = 1 TO li_end
		ln_item = la_contents[li_start]
		ln_item.getattributes(ls_attributes)
		li_col_count = upperbound( ls_attributes )
		ls_row = ''
		For li_col_start = 1 TO li_col_count
			ls_key = ls_attributes[li_col_start]
			ls_value = String(ln_item.getattribute(ls_attributes[li_col_start] ))
			//MessageBox("Info", ls_key + " : " + ls_value)
			ls_row = ls_row + ls_value
			IF li_col_start < li_col_count THEN ls_row = ls_row + ','
		NEXT
		//Messagebox("row", ls_row)
		write_log("d:\tss\output\report.csv", ls_row)
	next
end if

destroy ln_json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants