Welcome! Log In Create A New Profile

Advanced

[WM] - How to extract Array from JSON?

Posted by YogiYang 
[WM] - How to extract Array from JSON?
April 08, 2019 02:21PM
Hello,

I have facing a problem of extracting array which is returned in a JSON string from a server.

The content of JSON are:
[{"allowed_uoms":[399,268],"categ_id":[180,"C-PVC 45\u00b0 ELBOW"],"discount_value":55.0,"id":384,"list_price":8.1,"name":"\u00be\"","size_in_mm":0.0,"uom_data":[{"factor_inv":300.0,"id":399,"name":"BIG Pack Of 300"}]},{"allowed_uoms":[403,240],"categ_id":[180,"C-PVC 45\u00b0 ELBOW"],"discount_value":55.0,"id":385,"list_price":15.75,"name":"1\"","size_in_mm":0.0,"uom_data":[{"factor_inv":160.0,"id":403,"name":"BIG Pack Of 160"}]},{"allowed_uoms":[395,264],"categ_id":[180,"C-PVC 45\u00b0 ELBOW"],"discount_value":55.0,"id":386,"list_price":22.5,"name":"1\u00bc\"","size_in_mm":0.0,"uom_data":[{"factor_inv":80.0,"id":395,"name":"BIG Pack Of 80"}]}]

Here the item uom_data contains an array. How can we extract this array?

TIA

--
Yogi Yang
pao
Re: [WM] - How to extract Array from JSON?
April 08, 2019 04:45PM
Just use JSONTOVARIANT and the Occurrence property of the VariantMember

[doc.windev.com]
[doc.windev.com]

Regards

Paulo Oliveira
Re: [WM] - How to extract Array from JSON?
April 08, 2019 07:00PM
Hello Yog,

Use JSONToVariant()
vData is variant = JSONToVariant(jsonString)

nCount is int = vData.allowed_uoms..Occurrence //will let you know how many items you are working with

then to read it

for r = 1 to nCount
value = vData.allowed_uoms[r].discount_value 

     //Nested array
      nCntNest is int = vData.allowed_uoms[r].uom_data..Occurrence
      for e to nCntNest 
           NestedValue = vData.allowed_uoms[r].uom_data[e].factor_inv
      End

End

Good luck
DW

You have nested arrays inside so you will have to deal with them also.
Re: [WM] - How to extract Array from JSON?
April 08, 2019 07:05PM
pao Wrote:
-------------------------------------------------------
> Just use JSONTOVARIANT and the Occurrence property
> of the VariantMember
>
> [doc.windev.com]
> [doc.windev.com]


I have already referred to these help topics.

But they do not help in solving this problem.

Here is the code that I am using:
FOR EACH v OF JZ
		nProdID = v.id..Value
		sProdName = v.name..Value
		
		Info(v.uom_data..Type)  //<= this show type of variable to be 142 which is not listed anywhere on this page: [doc.windev.com]
		
 		FOR EACH X OF v.uom_data..member    //<== Get error here
			sProdPacking = X..Value
		END
		
		cyProdRate = v.list_price..Value
		rProdDIscount = v.discount_value..Value
		nProdQty = 0
	END

TIA
Re: [WM] - How to extract Array from JSON?
April 08, 2019 07:56PM
Hello,

Thanks everyone for your invaluable help.

Finally I have managed to get this working.

Here is the code snippet to help with the view to help others who may get stuck like me.

FOR EACH v OF JZ
		nProdID = v.id..Value
		sProdName = v.name..Value
		sProdPacking = v.uom_data[1].factor_inv
		nUomID = v.uom_data[1].id
		cyProdRate = v.list_price..Value
		rProdDIscount = v.discount_value..Value
		nProdQty = 0  //Will be entered buy Users
	END

TIA

--
Yogi Yang
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: