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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a JSON from VFP

Status
Not open for further replies.

DominicProgrammer

IS-IT--Management
May 14, 2015
11
0
0
KE
Hi Experts.

I have an issue here trying to create a json like txt file so i can use in in API link but i cant get the products coming in records [there are only 2 records in that temp table] why does the records duplicate in the ItemDetails object ? Kindly assist me. Thanks in advance:

Code:
Close All

lcCRLF = Chr(13)+Chr(10)
local krec,i
Use temp In 0
Select temp
Go Top

orec=1
i=1
SCAN
krec=Recno()
     Scatter Memvar
     kstockcode=stockcode
     Strtofile(['{"CallBackUrl":"[URL unfurl="true"]http://192.168.0.5:8128/kekaapi/invoices",[/URL]]+lcCRLF, [c:\json.txt], .F.)
     Strtofile(["InvoiceNumber":,] +'"'+Alltrim(m.docno)+'"'+','+ lcCRLF, [c:\json.txt], .T.)
     Strtofile(["TotalInvoiceAmount":,] +'"'+Alltrim(Str(m.amount))+'"'+','+ lcCRLF, [c:\json.txt], .T.)
     Strtofile(["TotalDiscount":,] +'"'+Alltrim(Str(m.discamt))+'"'+','+ lcCRLF, [c:\json.txt], .T.)
     Strtofile(["PINOfBuyer":,] +'P001244574Q'+','+ lcCRLF, [c:\json.txt], .T.)
     Strtofile(["HeaderFields":] +'{'+','+ lcCRLF, [c:\json.txt], .T.)

     Strtofile([		"Header1": "",] +''+ lcCRLF, [c:\json.txt], .T.)
     Strtofile([		"Header2": "",] +''+ lcCRLF, [c:\json.txt], .T.)
     Strtofile([		"Header3": "",] +''+ lcCRLF, [c:\json.txt], .T.)
     Strtofile([		"Header4": "",] +''+ lcCRLF, [c:\json.txt], .T.)
     Strtofile([		"Header5": "",] +''+ lcCRLF, [c:\json.txt], .T.)
     Strtofile([   },] +''+ lcCRLF, [c:\json.txt], .T.)
     **----------------Item details
     Strtofile(["ItemDetails":] +'[{ "HSCode"'+":"+','+ lcCRLF, [c:\json.txt], .T.)
     for i=1 to RECCOUNT()
          Strtofile([                              "ItemCode":,] +'"'+Alltrim(m.stockcode)+'"'+','+ lcCRLF, [c:\json.txt], .t.)
          Strtofile([                              "Description":,] +'"'+Alltrim(m.desc)+'"'+','+ lcCRLF, [c:\json.txt], .T.)
          Strtofile([                              "UnitInclPrice":,] +'"'+Alltrim(Str(m.unitprice))+'"'+','+ lcCRLF, [c:\json.txt], .T.)
          Strtofile([                              "UnitDiscount":,] +'"'+Alltrim(Str(m.discamt))+'"'+','+ lcCRLF, [c:\json.txt], .T.)
          Strtofile([                  },] +''+ lcCRLF, [c:\json.txt], .T.)
		 krec=krec+1     
     NEXT
     Strtofile([} ]+''+ lcCRLF, [c:\json.txt], .T.)
     Select temp
	 
Endscan
Modify File c:\json.txt
 
Hi,

Because your code duplicate they.

[pre]
Close All

lcCRLF = Chr(13)+Chr(10)
local krec,i
Use temp In 0
Select temp
Go Top

orec=1
i=1
SCAN && scan table from first record to last record ( RECCOUNT() )
krec=Recno()
Scatter Memvar
kstockcode=stockcode
Strtofile(['{"CallBackUrl":"]+lcCRLF, [c:\json.txt], .F.)
Strtofile(["InvoiceNumber":,] +'"'+Alltrim(m.docno)+'"'+','+ lcCRLF, [c:\json.txt], .T.)
Strtofile(["TotalInvoiceAmount":,] +'"'+Alltrim(Str(m.amount))+'"'+','+ lcCRLF, [c:\json.txt], .T.)
Strtofile(["TotalDiscount":,] +'"'+Alltrim(Str(m.discamt))+'"'+','+ lcCRLF, [c:\json.txt], .T.)
Strtofile(["PINOfBuyer":,] +'P001244574Q'+','+ lcCRLF, [c:\json.txt], .T.)
Strtofile(["HeaderFields":] +'{'+','+ lcCRLF, [c:\json.txt], .T.)

Strtofile([ "Header1": "",] +''+ lcCRLF, [c:\json.txt], .T.)
Strtofile([ "Header2": "",] +''+ lcCRLF, [c:\json.txt], .T.)
Strtofile([ "Header3": "",] +''+ lcCRLF, [c:\json.txt], .T.)
Strtofile([ "Header4": "",] +''+ lcCRLF, [c:\json.txt], .T.)
Strtofile([ "Header5": "",] +''+ lcCRLF, [c:\json.txt], .T.)
Strtofile([ },] +''+ lcCRLF, [c:\json.txt], .T.)
**----------------Item details
Strtofile(["ItemDetails":] +'[{ "HSCode"'+":"+','+ lcCRLF, [c:\json.txt], .T.)
for i=1 to RECCOUNT() && create item x records in table
Strtofile([ "ItemCode":,] +'"'+Alltrim(m.stockcode)+'"'+','+ lcCRLF, [c:\json.txt], .t.)
Strtofile([ "Description":,] +'"'+Alltrim(m.desc)+'"'+','+ lcCRLF, [c:\json.txt], .T.)
Strtofile([ "UnitInclPrice":,] +'"'+Alltrim(Str(m.unitprice))+'"'+','+ lcCRLF, [c:\json.txt], .T.)
Strtofile([ "UnitDiscount":,] +'"'+Alltrim(Str(m.discamt))+'"'+','+ lcCRLF, [c:\json.txt], .T.)
Strtofile([ },] +''+ lcCRLF, [c:\json.txt], .T.)
krec=krec+1
NEXT
Strtofile([} ]+''+ lcCRLF, [c:\json.txt], .T.)
Select temp

Endscan
Modify File c:\json.txt
[/pre]
 
Hi Jindrova.

Thanks for your reply, i appreciate, that duplicate is what i want to solve, help me, i have attached the Db too.

This is the JSON result i envision.

<'{"CallBackUrl":""InvoiceNumber":,"910641",
"TotalInvoiceAmount":,"0",
"TotalDiscount":,"0",
"PINOfBuyer":,P001244574Q,
"HeaderFields":{,
"Header1": "",
"Header2": "",
"Header3": "",
"Header4": "",
"Header5": "",
},
"ItemDetails":[{ "HSCode":,
"ItemCode":,"1010060107",
"Description":,"RAHA DRINKING CHOC SACHET 24X100G",
"UnitInclPrice":,"963",
"UnitDiscount":,"0",
},
"ItemCode":,"1010050306",
"Description":,"RAHA DRINKING CHOC.12*100 GM JARS(DOZ)",
"UnitInclPrice":,"963",
"UnitDiscount":,"0",
},
}
>


Regards.
 
Hmm, You don't see bug in your code...
You code contains many bugs (duplicite in ItemDetails, nonvalid JSON)


[pre]
Close All

lcCRLF = Chr(13)+Chr(10)
Use temp In 0
Select temp
Go Top

Scatter Memvar
Strtofile([{"CallBackUrl":"]+lcCRLF, [c:\json.txt], .F.)
Strtofile(["InvoiceNumber":] +'"'+Alltrim(m.docno)+'"'+','+ lcCRLF, [c:\json.txt], .T.)
Strtofile(["TotalInvoiceAmount":] +'"'+Alltrim(Str(m.amount))+'"'+','+ lcCRLF, [c:\json.txt], .T.)
Strtofile(["TotalDiscount":] +'"'+Alltrim(Str(m.discamt))+'"'+','+ lcCRLF, [c:\json.txt], .T.)
Strtofile(["PINOfBuyer":"] +'P001244574Q'+'",'+ lcCRLF, [c:\json.txt], .T.)
Strtofile(["HeaderFields":] +'{'+''+ lcCRLF, [c:\json.txt], .T.)
Strtofile([ "Header1": "",] + lcCRLF, [c:\json.txt], .T.)
Strtofile([ "Header2": "",] + lcCRLF, [c:\json.txt], .T.)
Strtofile([ "Header3": "",] + lcCRLF, [c:\json.txt], .T.)
Strtofile([ "Header4": "",] + lcCRLF, [c:\json.txt], .T.)
Strtofile([ "Header5": ""] + lcCRLF, [c:\json.txt], .T.)
Strtofile([ },] + lcCRLF, [c:\json.txt], .T.)

**----------------Item details
Strtofile(["ItemDetails":] +'['+ lcCRLF, [c:\json.txt], .T.)

SCAN && scan table from first record to last record ( RECCOUNT() )

Scatter Memvar
Strtofile('{ "HSCode":"",'+ lcCRLF, [c:\json.txt], .T.)
Strtofile([ "ItemCode":] +'"'+Alltrim(m.stockcode)+'"'+','+ lcCRLF, [c:\json.txt], .t.)
Strtofile([ "Description":] +'"'+Alltrim(m.desc)+'"'+','+ lcCRLF, [c:\json.txt], .T.)
Strtofile([ "UnitInclPrice":] +'"'+Alltrim(Str(m.unitprice))+'"'+','+ lcCRLF, [c:\json.txt], .T.)
Strtofile([ "UnitDiscount":] +'"'+Alltrim(Str(m.discamt))+'"'+ lcCRLF, [c:\json.txt], .T.)
Strtofile([ }]+IIF(RECNO()<RECCOUNT(), ',', '')+lcCRLF, [c:\json.txt], .T.)

Endscan
Strtofile(']}'+ lcCRLF, [c:\json.txt], .T.)

Modify File c:\json.txt
[/pre]

MartinaJ
 
Hi Martinaj.

Yours worked. Thanks thanks thanks ,About the Json invalidation i will try work on it.

Regards.
Dominic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top