Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem in converting JSON to DBF using nfJsonopen

Status
Not open for further replies.

Sng1

Programmer
Aug 3, 2021
65
IN
Below Code is creating property name -> exp_vfpsafe_ instead of exp whereas JSON does not contain exp_vfpsafe_
Code:
Close All

TEXT TO Json
{"data":{"exp":[{"inv":[{"val":156420,"itms":[{"csamt":0,"rt":0,"txval":156420,"iamt":0}],
"flag":"N","sbnum":"3206654","idt":"29-07-2022","sbdt":"01-08-2022","inum":"HMEI01",
"chksum":"aa9b7668f49a3f0f0d1d9354eb07afa77447c2fc38d23703a1656894d1d8da67","sbpcode":"INAMD4"}],
"exp_typ":"WOPAY"}]},"status_cd":"1","status_desc":"GSTR request succeeds",
"header":{"gst_username":"hariommanuf","state_cd":"02","ip_address":"192.168.0.1",
"txn":"f2f56579fef545cdad7898ac2cfe3d4e","client_id":"GSP2c426d19-739c-4ca2-9681-27b2584d09df",
"client_secret":"GSP5b0a2385-ac74-400f-9c19-fc365a32e244","ret_period":"072022","gstin":"02AABTH0219D1ZK"}}
ENDTEXT

jsonv = '['+Alltrim(json)+']'
Create Cursor Exp(exp_type c(5),inum c(20),idt c(10),Val N(16,2),sbpcode c(6),;
	sbnum c(7),sbdt c(10),diff_percent N(5,2),rt N(6,2),txval N(14,2),;
	iamt N(14,2),csamt N(14,2),json m(4),brgstin c(15),taxperiod c(15),;
	z N(2),idate d(8),pkey N(6),fp c(6),portal l(1),gkey N(7),;
	taxyear N(4),hsn N(8))


Set Procedure To d:\ERP\Utilities\Gst\nfJson-Master\nfJson\nfjsonread.prg
oJSON = nfjsonread(jsonv)
Local i,rowv
tnLevel = 0

For Each Gstr1Crit In oJSON.Array
	tnLevel = Evl(tnLevel, 0)

	Local lnI, lvMember, laMembers[1]
	For lnI=1 To Amembers(laMembers,Gstr1Crit )
		lvMember = Getpem(Gstr1Crit,laMembers[lnI])
		If Vartype(lvMember)="O" And Capt(laMembers[lnI]) = 'DATA'
			For Each Expv In lvMember.Exp
				For Each invoicev In Expv.Inv
					For Each ItemDtl In invoicev.Itms
						Select Exp
						Append Blank
						Gather Name lvMember
						Gather Name Ctinv
						Gather Name invoicev
						Gather Name ItemDtl
						Browse Width 15
					Endfor
				Endfor
			Endfor

		Endif
	Endfor
Endfor
 
That's because EXP is a native VFP language function and so you can't name an array or array property exp in VFP.

The json parser therefore has to modify that name to enable adding it to the VFP object as array property.

You now can adapt to that intentional change and change lvMember.Exp in your code to lvMember.Exp_vfpsafe_.

By the way, your cursor name can stay EXP, though it is bad style to use reserved words for alias names. If the json is under your control, you'd also change that names, as it's read into an array property and that can't be named exp, but nfjson detects that (it tries the given name) and changes to the safe name with the _vfpsafe_ suffix.

Try for yourself:
Code:
_screen.addproperty('exp[1]')
This results in error 'incorrect property name'. So the json name can't be used for creating a VFP object similar to the javascript object that this json would create in JS.

Edit: To be very clear, you can't create an exp array property, so you have to live with that alternative name. It doesn't come from json, of course, it also isn't added by VFP itself as VFP simply errors and refuses this and also other names of functions. But VFP errors, it doesn't make an automatic name change as SL does with "expr1" or such names for expressions you don't name. So where does it come from?

This part of nfjsonread.prg does it:
Code:
Try
   AddProperty(obj,(m.arrayname+'(1)'),.Null.)
Catch
   m.arrayname = m.arrayname+'_vfpSafe_'
   AddProperty(obj,(m.arrayname+'(1)'),.Null.)
Endtry
You could change the catch part to make another name change than suffixing _vfpsafe_, but you can't have exp. I haven't looked into whether JS even allows characters VFP can't cope with, which would make a more general renaming necessary. I know atlopes also has a GIT repository about this topic of names that have varying ranges of allowed characters and challenges about disallowed names. I guess in JS you could make use of almost any UTF8 character which VFP would choke at, still after adding '_vfpsafe_'. So there is even room for improvement here, I guess atlopes did not want yet another dependency on the names repository for usual json names are the Latin/English letters only and so adding '_vfpsafe_' removes it from any reserved word in VFP in something like 99.9% of all cases.

Well, feel free to just add '_' for example, instead of '_vfpsafe_'. Otherwise you could change the JSON "exp" to "expression", "experience", "expiration", "exposition", "expenditure" or whatever it actually should mean.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top