How do i create a Json from a vfp table? - json

I have an issue 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?
{
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":"http://192.168.0.5:8128/kekaapi/invoices",]+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
}

I can see a couple of issues.
In the ItemDetails section you have the for loop:
for i=1 to RECCOUNT().
RECCOUNT() gives you the number of rows in your table, so if your table has 2 rows, you'll get 2 sets of ItemDetails. As the SCATTER MEMVAR command is outside the for loop, the contents of the MEMVARs (m.StockCode etc) won't change, so you'll get 2 copies of the same values. If you only want 1 set of ItemDetails, remove the for loop. If you want multiple ItemDetails, you'll need to get them into the MEMVARs within the for loop.
When the SCAN loops to the 2nd table row, the first call to StrToFile() has .F. as the last parameter so it overwrites the file c:\json.txt. This file then gets recreated with the new rows values.
So I would expect the file to contain the values of the last record in the table with the ItemDetails duplicated for every record in the table(but the values will be from the last row values).
The solution is to move the first StrToFile() call BEFORE the SCAN (if you want CallBackUrl row) or change the .F. to .T. and add code to delete the file before the SCAN (if you want a CallBackUrl line for each row).
I suspect you want the last call to StrToFile() after the ENDSCAN too.
Hope this helps.

Related

Is there a way to return a nested query result as a JSON object/array? [duplicate]

I need to create a stored procedure in mysql that take some fields from DB and create a neasted json object:
Firstly I create a json object as showing below:
{
"form": "Exams tests",
"version": "v3.001.01",
"questions": []
}
And secondly a json array object like this:
{[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
And the result query showld be something like this:
{
"form": "Exams tests",
"version": "v3.001.01",
"questions": {[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
}
I got an error when I'm trying to insert a nested json array into a json object
I joined JSON_OBJECT with json_arrayagg as below:
{
"name": "Moon",
"resume": "This is a resume.",
"dt_ini": "2018-09-01",
"dt_end": null,
"cases": [
{
"unit": "unit 1",
"unit_id": 10
},
{
"unit": "unit 2",
"unit_id": 290
},
{
"unit": "unit 3",
"unit_id": 44
},
{
"unit": "unit 4",
"unit_id": 108
}
]
}
The final result is this:
CREATE DEFINER=`root`#`localhost` PROCEDURE `get_list_case`(
IN `code` int,
IN `base_code` int)
BEGIN
DECLARE json TEXT DEFAULT '';
SELECT JSON_OBJECT(
'name', v.name,
'dt_ini', v.dt_ini,
'dt_end', v.dt_end,
'resumo', v.resumo,
'cases', ( select json_arrayagg(json_object(
'unit_id',`tb_unit`.`unit_id`,
'unit',`tb_unit`.`unit`))
from tb_unit_case
INNER JOIN tb_unit ON tb_unit_case.unid_code = tb_unit.unit_id
WHERE tb_unit_case.case_code = code)
) INTO json
FROM v_case AS v
WHERE v.code = code and v.base_code = base_code;
SELECT json;
END
DECLARE json TEXT DEFAULT '';
DECLARE code INTEGER;
SET code = 1;
SELECT JSON_OBJECT(
'form', v.form_name,
'version', v.version,
'questions, ( select json_arrayagg(json_object(
'ordem',`tb_questions`.`order`,
'num_questions',`tb_questions`.`num`
'question1',`tb_questions`.`question1`
'answer1',`tb_questions`.`answer1`
))
from tb_questions
WHERE tb_questions.code = code)
) INTO json
FROM v_case AS v
WHERE v.code = code;
This is not an array, you have to remove {}
{[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}

How can I create a counter for certain values in a list? for google appscript

[Yes, Yes, Yes, Yes, Yes, Yes, No, Yes, Yes, No, Yes] this list I need to count and assign a variable the amount of Yes's there are in this list and another variable the amount of No's
I believe your goal as follows.
You want to retrieve the number of Yes and No from the array of ["Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "No", "Yes"] using Google Apps Script.
For this, how about this answer?
Pattern 1:
In this pattern, the for loop is used.
Sample script:
var ar = ["Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "No", "Yes"];
var obj = {};
for (var i = 0; i < ar.length; i++) {
var e = ar[i];
if (obj[e]) {
obj[e] += 1;
} else {
obj[e] = 1;
}
}
Logger.log(obj); // {"Yes":9,"No":2}
Test run:
var ar = ["Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "No", "Yes"];
var obj = {};
for (var i = 0; i < ar.length; i++) {
var e = ar[i];
if (obj[e]) {
obj[e] += 1;
} else {
obj[e] = 1;
}
}
console.log(obj); // {"Yes":9,"No":2}
console.log(obj.Yes)
console.log(obj.No)
Pattern 2:
In this pattern, the reduce is used.
Sample script:
const ar = ["Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "No", "Yes"];
const obj = ar.reduce((o, e) => Object.assign(o, {[e]: (o[e] ? o[e] + 1 : 1)}), {});
console.log(obj); // {"Yes":9,"No":2}
In this case, the spread syntax can also be used like {...o, [e]: (o[e] ? o[e] + 1 : 1)}. But the process cost of the spread syntax is much higher than that of Object.assign. So here, I proposed Object.assign. Ref
Test run:
const ar = ["Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "No", "Yes"];
const obj = ar.reduce((o, e) => Object.assign(o, {[e]: (o[e] ? o[e] + 1 : 1)}), {});
console.log(obj);
console.log(obj.Yes)
console.log(obj.No)
Note:
In the case of above scripts, The values of Yes and No can be retrieved with obj.Yes and obj.No, respectively.
About pattern 2, please use V8.
References:
Loops and iteration
reduce()
Object.assign()
Benchmark: Process Costs under V8 using Google Apps Script

Create a MySQL procedure that result in a nested JSON Array Object

I need to create a stored procedure in mysql that take some fields from DB and create a neasted json object:
Firstly I create a json object as showing below:
{
"form": "Exams tests",
"version": "v3.001.01",
"questions": []
}
And secondly a json array object like this:
{[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
And the result query showld be something like this:
{
"form": "Exams tests",
"version": "v3.001.01",
"questions": {[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
}
I got an error when I'm trying to insert a nested json array into a json object
I joined JSON_OBJECT with json_arrayagg as below:
{
"name": "Moon",
"resume": "This is a resume.",
"dt_ini": "2018-09-01",
"dt_end": null,
"cases": [
{
"unit": "unit 1",
"unit_id": 10
},
{
"unit": "unit 2",
"unit_id": 290
},
{
"unit": "unit 3",
"unit_id": 44
},
{
"unit": "unit 4",
"unit_id": 108
}
]
}
The final result is this:
CREATE DEFINER=`root`#`localhost` PROCEDURE `get_list_case`(
IN `code` int,
IN `base_code` int)
BEGIN
DECLARE json TEXT DEFAULT '';
SELECT JSON_OBJECT(
'name', v.name,
'dt_ini', v.dt_ini,
'dt_end', v.dt_end,
'resumo', v.resumo,
'cases', ( select json_arrayagg(json_object(
'unit_id',`tb_unit`.`unit_id`,
'unit',`tb_unit`.`unit`))
from tb_unit_case
INNER JOIN tb_unit ON tb_unit_case.unid_code = tb_unit.unit_id
WHERE tb_unit_case.case_code = code)
) INTO json
FROM v_case AS v
WHERE v.code = code and v.base_code = base_code;
SELECT json;
END
DECLARE json TEXT DEFAULT '';
DECLARE code INTEGER;
SET code = 1;
SELECT JSON_OBJECT(
'form', v.form_name,
'version', v.version,
'questions, ( select json_arrayagg(json_object(
'ordem',`tb_questions`.`order`,
'num_questions',`tb_questions`.`num`
'question1',`tb_questions`.`question1`
'answer1',`tb_questions`.`answer1`
))
from tb_questions
WHERE tb_questions.code = code)
) INTO json
FROM v_case AS v
WHERE v.code = code;
This is not an array, you have to remove {}
{[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}

How to insert string in JSON and sort alphabetically file using shell script

let say i have JSON file and i have to insert two new string ccddff-live and ccddff-preview which has to inserted into the below JSON file via shell script and order alphabetically
before inserting
{``
"bbccdd-live": {
"master_url": "http://master.solar.local:8083/solr/bbccdd-live",
"poll_interval": "00:01:00"
},
}
After Inserting
"bbccdd-live": {
"master_url": "http://master.solar.local:8083/solr/bbccdd-live",
"poll_interval": "00:01:00"
},
"bbccdd-preview": {
"master_url": "http://master.solar.local:8083/solr/bbccdd-preview",
"poll_interval": "00:01:00"
},
"ccddee-live": {
"master_url": "http://master.solar.local:8083/solr/ccddee-live",
"poll_interval": "00:01:00"
},
"ccddee-preview": {
"master_url": "http://master.solar.local:8083/solr/ccddee-preview",
"poll_interval": "00:01:00"
}

Using a custom function in a Laravel 5 Controller

I'm using a Laravel 5, I just want to know how to use or declare a custom function inside my controller. I have
class UploadController extends Controller
{
function on_request_done($content, $url, $ch, $search) {
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode !== 200) {
print "Fetch error $httpcode for '$url'\n";
return;
}
$responseobject = json_decode($content, true);
if (empty($responseobject['responseData']['results'])) {
print "No results found for '$search'\n";
return;
}
print "********\n";
print "$search:\n";
print "********\n";
$allresponseresults = $responseobject['responseData']['results'];
foreach ($allresponseresults as $responseresult) {
$title = $responseresult['title'];
print "$title\n";
}
}
}
And whenever I call on_request_done inside one of my functions let's say,
public function getParallelApi()
{
define ('SEARCH_URL_PREFIX', 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&filter=0');
$terms_list = array(
"John", "Mary",
"William", "Anna",
"James", "Emma",
"George", "Elizabeth",
"Charles", "Margaret",
"Frank", "Minnie",
"Joseph", "Ida",
"Henry", "Bertha",
"Robert", "Clara",
"Thomas", "Alice",
"Edward", "Annie",
"Harry", "Florence",
"Walter", "Bessie",
"Arthur", "Grace",
"Fred", "Ethel",
"Albert", "Sarah",
"Samuel", "Ella",
"Clarence", "Martha",
"Louis", "Nellie",
"David", "Mabel",
"Joe", "Laura",
"Charlie", "Carrie",
"Richard", "Cora",
"Ernest", "Helen",
"Roy", "Maude",
"Will", "Lillian",
"Andrew", "Gertrude",
"Jesse", "Rose",
"Oscar", "Edna",
"Willie", "Pearl",
"Daniel", "Edith",
"Benjamin", "Jennie",
"Carl", "Hattie",
"Sam", "Mattie",
"Alfred", "Eva",
"Earl", "Julia",
"Peter", "Myrtle",
"Elmer", "Louise",
"Frederick", "Lillie",
"Howard", "Jessie",
"Lewis", "Frances",
"Ralph", "Catherine",
"Herbert", "Lula",
"Paul", "Lena",
"Lee", "Marie",
"Tom", "Ada",
"Herman", "Josephine",
"Martin", "Fanny",
"Jacob", "Lucy",
"Michael", "Dora",
);
if (isset($argv[1])) {
$max_requests = $argv[1];
} else {
$max_requests = 10;
}
$curl_options = array(
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_USERAGENT, 'Parallel Curl test script',
);
$parallel_curl = new ParallelCurl($max_requests, $curl_options);
foreach ($terms_list as $terms) {
$search = '"'.$terms.' is a"';
$search_url = SEARCH_URL_PREFIX.'&q='.urlencode($terms);
$parallel_curl->startRequest($search_url, 'on_request_done', $search);
}
$parallel_curl->finishAllRequests();
//return "Helloo";
}
I get
call_user_func() expects parameter 1 to be a valid callback, function 'on_request_done' not found or invalid function name
My route
Route::get('/parallelapi', 'UploadController#getParallelApi');
PLEASE NOTE THAT getParallelApi() is under UploadController as well.
The problem is you are passing in the name of the function but it has no idea what class that function exists in. You should be able to do this by passing an array as your callback with the first item being an instance of your controller and the second item being the name of the function.
$parallel_curl->startRequest($search_url, [$this, 'on_request_done'], $search);