Source language: Translate to:

JSON TO CSV

General questions about NeoAppBuilder - our rapid application development tool for building HTML5, web and mobile apps.

Moderator: Neosoft Support

JSON TO CSV

Postby Wrangler » Sat Sep 10, 2016 1:24 pm

Getting into Neoappbuilder, which means getting into javascript. I need help with converting json to csv from a variable and saving it to a local file, or better yet converting and displaying on the fly:

.get json
BeginJS
var req = new XMLHttpRequest();
req.open('GET', 'https://www.cannabisreports.com/api/v1.0/strains/VUJCJ4TYMG000000000000000', false);
req.send(null);
if (req.readyState==4);
$rootScope.TestVar = req.responseText;
$rootScope.TestArray = $rootScope.TestVar.split("\r\n");
EndJS

AlertBox "JSON" "[TestVar]" ""

The json data is now stored in the [testvar] variable and ready to be converted. Can anyone help with this code?
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1506
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Re: JSON TO CSV

Postby Gaev » Sat Sep 10, 2016 3:57 pm

Wrangler:

Can anyone help with this code?

Since JSON (Javascript Object Notation) describes Objects (akin to relational databases) while CSV is more like unrelated records, in most cases, you can not do a generic transformation.

I copied this url (https://www.cannabisreports.com/api/v1. ... 0000000000) into my Browser ... and then used my text editor to split the result into human readable format ...
Code: Select all
{
   
"data":
{
"name":"Jack Herer",
"ucpc":"VUJCJ4TYMG000000000000000",
"link":"https:\/\/www.cannabisreports.com\/api\/v1.0\/strains\/VUJCJ4TYMG000000000000000",
"qr":"https:\/\/www.cannabisreports.com\/strain-reports\/sensi-seeds\/jack-herer\/qr-code.svg",
"url":"https:\/\/www.cannabisreports.com\/strain-reports\/sensi-seeds\/jack-herer",
"image":"https:\/\/www.cannabisreports.com\/images\/strains\/a\/full_ae82eef3bb2b7694df7f8bfa0da22a5d2b0f3689.jpg",

"seedCompany":
{
"name":"Sensi Seeds",
"ucpc":"VUJCJ00000000000000000000",
"link":"https:\/\/www.cannabisreports.com\/api\/v1.0\/seed-companies\/VUJCJ00000000000000000000"
},

"genetics":
{
"names":"(Northern Lights #5 x Skunk #1) x Haze",
"ucpc":"(VUJCJ4MPQ2000000000000000 x VUJCJY6ZUC000000000000000) x 9XVU7WJQCD000000000000000",
"link":"https:\/\/www.cannabisreports.com\/api\/v1.0\/strains\/VUJCJ4TYMG000000000000000\/genetics"
},

"lineage":
{
"Afghanistan":"AF",
"Colombia":"CO",
"India":"IN",
"Mexico":"MX",
"Thailand":"TH"
},

"children":
{
"count":159,
"link":"https:\/\/www.cannabisreports.com\/api\/v1.0\/strains\/VUJCJ4TYMG000000000000000\/children"
},

"reviews":
{
"count":9,
"link":"https:\/\/www.cannabisreports.com\/api\/v1.0\/strains\/VUJCJ4TYMG000000000000000\/reviews"
},

"createdAt":
{
"datetime":"2008-08-15 19:20:00",
"timezone":"UTC"
},

"updatedAt":
{
"datetime":"2015-03-11 23:10:50",
"timezone":"UTC"
}

}  //end of data object

}


So, instead of doing ...
Code: Select all
$rootScope.TestVar = req.responseText;
... you can try ...
Code: Select all
myData = req.responseText.data;

myName = myData.name; // variable myName will contain "Jack Herer"


Similarly for ucpc,link,qr,url and image

But then things start to get tricky ...
Code: Select all
mySeedCompany = myData.seedCompany;

mySeedCompanyName = mySeedCompany.name; // variable will contain "Sensi Seeds"
mySeedCompanyUcpc = mySeedCompany.ucpc; // variable will contain "VUJCJ00000000000000000000"
etc .etc.


Hope you get the point.

Please note that each instance of Objects does not have to have every key/value present within it ... so the above demo code can become a lot more complicated if you need to check for presence of desired keys/values.
User avatar
Gaev
 
Posts: 3728
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: JSON TO CSV

Postby Wrangler » Mon Sep 12, 2016 10:42 am

Thanks for the response, Gaev.

Adjusting the code as you recommended:

.get json
BeginJS
var req = new XMLHttpRequest();
req.open('GET', 'https://www.cannabisreports.com/api/v1.0/strains/VUJCJ4TYMG000000000000000', false);
req.send(null);
if (req.readyState==4);
myData = req.responseText.data;
myName = myData.name; // variable myName will contain "Jack Herer"
$rootScope.TestArray = $rootScope.myName.split("\r\n");
EndJS

Gives this error:

TypeError: Unable to get property 'name' of undefined or null reference
at $scope.PushButton1_click (file:///C:/Users/admin/Documents/NeoAppBuilder/read-file-on-server_Test/main.js?v=12:19:1)
at Anonymous function (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:200:52)
at Anonymous function (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular-touch.min.js:12:172)
at m.prototype.$eval (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:126:239)
at m.prototype.$apply (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:126:452)
at Anonymous function (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular-touch.min.js:12:152)
at m.event.dispatch (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/jquery.min.js:4:8493)
at r.handle (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/jquery.min.js:4:5177)

Does it need to be in human readable format?
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1506
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Re: JSON TO CSV

Postby Gaev » Mon Sep 12, 2016 1:10 pm

Wrangler:

Upon closer examination, I see that your webpage returns data as a text/string i.e. the object is already stringify()'ed ... this is not apparent just by looking at the response in the Browser.

So, you need to JSON.parse() it to convert it to an object.

So, replace this line ...
Code: Select all
myData = req.responseText.data;

... with ...
Code: Select all
responseObject = JSON.parse(req.responseText);
myData = responseObject.data;
User avatar
Gaev
 
Posts: 3728
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: JSON TO CSV

Postby Wrangler » Mon Sep 12, 2016 1:21 pm

Thanks, Gaev.

No more errors, but the variable myName returns Undefined:

.get json
BeginJS
var req = new XMLHttpRequest();
req.open('GET', 'https://www.cannabisreports.com/api/v1.0/strains/VUJCJ4TYMG000000000000000', false);
req.send(null);
if (req.readyState==4);
responseObject = JSON.parse(req.responseText);
myData = responseObject.data;
myName = myData.name; // variable myName will contain "Jack Herer"

EndJS

AlertBox "" "[myName]" ""

This code is used in a button. Is there something I missed?
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1506
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Re: JSON TO CSV

Postby Gaev » Mon Sep 12, 2016 3:57 pm

Wrangler:

the variable myName returns Undefined

That is because the variable myName in ...
Code: Select all
myName = myData.name;

... is a Javascript variable ... where as [myName] is a NeoApp variable ... you have to copy all Javascript variables to the other side using commands like ...
Code: Select all
$rootScope.myName = myName;

... and then ...
Code: Select all
AlertBox "" "[myName]" ""
... will show you the value.

Note that you can also use ...
Code: Select all
alert(myName);
... within the BeginJS/EndJS to have the value of the Javascript variable displayed.
User avatar
Gaev
 
Posts: 3728
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: JSON TO CSV

Postby Wrangler » Tue Sep 13, 2016 9:56 am

Got it, Gaev. Thanks much. Now on to grabbing the rest of the data....
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1506
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Re: JSON TO CSV

Postby Wrangler » Tue Sep 13, 2016 11:09 am

Gaev. Do me a favor and run the following code in a button.

Code: Select all
.get json
BeginJS
 var req = new XMLHttpRequest();
  req.open('GET', 'https://www.cannabisreports.com/api/v1.0/strains/VUJCJ4TYMG000000000000000', false);
  req.send(null);
  if (req.readyState==4);
responseObject = JSON.parse(req.responseText);
myData = responseObject.data;

.Get strain name
myName = myData.name; // variable myName will contain "Jack Herer"
$rootScope.myName = myName;

.Get ucpc number of strain
ucpc = myData.ucpc;
$rootScope.ucpc = ucpc;

.Get breeder name
mySeedCompany = myData.seedCompany;

mySeedCompanyName = mySeedCompany.name; // variable will contain "Sensi Seeds"
$rootScope.mySeedCompanyName = mySeedCompanyName;
EndJS

AlertBox "" "Strain Name: [myName]<BR>UCPC: [ucpc]<BR>Breeder: [mySeedCompanyName]" ""


See if you get the following error:

Syntax error (main.js?v=9, 21)
[$injector:modulerr] http://errors.angularjs.org/1.3.16/$inj ... p&p1=Error: [$injector:nomod] http://errors.angularjs.org/1.3.16/$inj ... ?p0=NeoApp
at Anonymous function (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:21:406)
at a (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:21:34)
at Anonymous function (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:21:289)
at Anonymous function (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:35:44)
at q (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:7:320)
at g (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:34:399)
at bb (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:38:133)
at d (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:17:379)
at xc (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:18:172)
at Od (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:17:1) (angular.min.js, 35)

Everything working fine, then errors. I want to see if I have reached the API test limit (waiting for official API key)

Thanks much.
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1506
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Re: JSON TO CSV

Postby Gaev » Tue Sep 13, 2016 1:27 pm

Wrangler:

Syntax error (main.js?v=9, 21)
[$injector:modulerr] http://errors.angularjs.org/1.3.16/$inj ... p&p1=Error: [$injector:nomod] http://errors.angularjs.org/1.3.16/$inj ... ?p0=NeoApp
at Anonymous function (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:21:406)
at a (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:21:34)
at Anonymous function (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:21:289)
at Anonymous function (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:35:44)
at q (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:7:320)
at g (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:34:399)
at bb (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:38:133)
at d (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:17:379)
at xc (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:18:172)
at Od (file:///C:/ProgramData/NeoSoft/NeoAppBuilder/js/angular.min.js:17:1) (angular.min.js, 35)

Everything working fine, then errors. I want to see if I have reached the API test limit (waiting for official API key)

Syntax Error shows up BEFORE you even click on the Button ... which means that the code did NOT compile properly ... nothing to do with API key at server site.

The Syntax Error it is complaining about are the comment lines ... in Javascript, they begin with // (not a dot) ... this code works as expected ...
Code: Select all
 
BeginJS
var req = new XMLHttpRequest();
req.open('GET', 'https://www.cannabisreports.com/api/v1.0/strains/VUJCJ4TYMG000000000000000', false);
req.send(null);
if (req.readyState==4)
{
responseObject = JSON.parse(req.responseText);
myData = responseObject.data;

//Get strain name
myName = myData.name; // variable myName will contain "Jack Herer"
$rootScope.myName = myName;

//Get ucpc number of strain
ucpc = myData.ucpc;
$rootScope.ucpc = ucpc;

//Get breeder name
mySeedCompany = myData.seedCompany;

mySeedCompanyName = mySeedCompany.name; // variable will contain "Sensi Seeds"
$rootScope.mySeedCompanyName = mySeedCompanyName;

}
EndJS

AlertBox "" "Strain Name: [myName]<BR>UCPC: [ucpc]<BR>Breeder: [mySeedCompanyName]" ""
User avatar
Gaev
 
Posts: 3728
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: JSON TO CSV

Postby Gaev » Tue Sep 13, 2016 1:31 pm

Wrangler:

Forgot to mention ... take special note of how to code if commands in Javascript ...
Code: Select all
if (age > 21)
{
   alert("Adult");
}
else
{
   alert("Child");
}
User avatar
Gaev
 
Posts: 3728
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: JSON TO CSV

Postby Wrangler » Tue Sep 13, 2016 3:33 pm

Yup. The comment usage got me. And I've made note of using { } in an if statement. Does this also apply to other conditional statements, such as while, loop etc?

Thanks a lot!
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1506
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Re: JSON TO CSV

Postby Gaev » Tue Sep 13, 2016 3:53 pm

User avatar
Gaev
 
Posts: 3728
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada


Return to General NeoAppBuilder Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron