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

how do I read json array which has no root entry #3

Open
prashantnirgun opened this issue Aug 19, 2022 · 3 comments
Open

how do I read json array which has no root entry #3

prashantnirgun opened this issue Aug 19, 2022 · 3 comments

Comments

@prashantnirgun
Copy link

my json array response is as follows
[{"route":"Transaction","service_status":"Active","balance":82,"validity":"2022-08-11T18:30:00.000Z"},{"route":"Promotional","service_status":"Active","balance":100,"validity":"2022-12-30T18:30:00.000Z"}]

01 How do I access 2nd row and balance attribute
02 Is there is any way I can determine do I have how many rows I mean it might have one or more rows

I tried out this but didn't works
ln_json.retrieve("/1/route", ref la_data)

@xlat
Copy link
Owner

xlat commented Aug 21, 2022

Hi, here is one way to do this :

ls_error = ln_json.parse('[{"route":"Transaction","service_status":"Active","balance":82,"validity":"2022-08-11T18:30:00.000Z"},{"route":"Promotional","service_status":"Active","balance":100,"validity":"2022-12-30T18:30:00.000Z"}]')
if ls_error = "" then
	if ln_json.isarray() then
		any la_array[]
		la_array[] = ln_json.getarray()
		messagebox("array length:", string( upperbound( la_array[] ) ) )
		ln_json.retrieve("1/route", ref la_data)
		string ls_route
		ls_route = la_data
		messagebox("1st route:", ls_route)
	end if
end if

@prashantnirgun
Copy link
Author

Thanks it works now had one more I if I had array as a element which hold array my code is as follows is there any better way to handle this?

int li_start, li_end
String ls_error, ls_response, ls_value
any la_data
any la_value
json ln_json

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) 
	li_end = upperbound( la_data[])
	For li_start = 1 TO li_end
		ln_json.retrieve("contents/" + String(li_start) + "/name", ref la_value) 
		MessageBox("Info", "Name : " + String(la_value))
	NEXT
end if

destroy ln_json

@xlat
Copy link
Owner

xlat commented Aug 23, 2022

Hi, here is another way to do the same, using less retrieve() calls, so for huge array it should be faster:

int li_start, li_end
String ls_error, ls_response
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[] )
	For li_start = 1 TO li_end
		ln_item = la_contents[li_start]
		MessageBox("Info", "Name : " + string(ln_item.getattribute("name")))
	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