Using combination of JSON.stringify and encodeURIComponent - javascript

So I hava a JSON stored in a variable
var x = '{"time":"Friday, January 15, 2016 9:13 PM","keywords":"social","tweets_people":[{"text":"#FreeformTV I want to see what my social DNA looks like! #FreeformLaunch","username":"bbjeagle","user_image":"http://pbs.twimg.com/profile_images/681285613526515712/H6aB4gS__normal.jpg","timestamp":"2016-01-15T15:42:59.000Z","display_name":"JOY","language":"en","tweet_id":"688023580240670720","location":"united_states","location_details":{"country":"united_states","type":"country"},"retweet_count":0,"reach":531,"favorite_count":0,"sentiment":84,"entities":{}},{"text":"#IBM Scoops Up IRIS Analytics Fraud Detection Firm https://t.co/6oisTfWGNq #technology #enterprise #social #tc","username":"chriswrighttech","user_image":"http://pbs.twimg.com/profile_images/577224176542445568/1N2FMNHV_normal.jpg","timestamp":"2016-01-15T15:42:55.000Z","display_name":"Chris Wright","language":"en","tweet_id":"688023567221534721","location":null,"location_details":null,"retweet_count":0,"reach":458,"favorite_count":0,"sentiment":-50,"entities":{"urls":[{"display_url":"goo.gl/fb/weklj1","expanded_url":"http://goo.gl/fb/weklj1","url":"https://t.co/6oisTfWGNq"}]}},{"text":"Handbook of Statistical Modeling for the Social and Behavioral Sciences https://t.co/qCbResQLzK https://t.co/sLf2uhhUQd","username":"allwood_mary","user_image":"http://pbs.twimg.com/profile_images/653044576501035008/VWEwjp_N_normal.jpg","timestamp":"2016-01-15T15:42:49.000Z","display_name":"Mary Allwood","language":"en","tweet_id":"688023541023924227","location":null,"location_details":null,"retweet_count":0,"reach":52,"favorite_count":0,"sentiment":34,"entities":{"urls":[{"display_url":"uae-trip.info/p/tr/?query=ht…","expanded_url":"http://uae-trip.info/p/tr/?query=http://rover.ebay.com/rover/1/711-53200-19…=2&toolid=10039&campid=5337597384&item=151951539608&vectorid=229466&lgeo=1","url":"https://t.co/qCbResQLzK"}],"photo":[{"display_url":"pic.twitter.com/sLf2uhhUQd","expanded_url":"http://twitter.com/allwood_mary/status/688023541023924227/photo/1","type":"photo","media_url":"http://pbs.twimg.com/media/CYxZw96WsAAzzvk.jpg","resolution":{"w":242,"h":400,"resize":"fit"},"url":"https://t.co/sLf2uhhUQd"}]}},{"text":"Philasmicos R5 CRM (Standard) #Business Social Networking #Mac App... https://t.co/88YCSejyyE #socialnetworking","username":"marac00per","user_image":"http://pbs.twimg.com/profile_images/509110528208142336/Ah4ZC3vh_normal.jpeg","timestamp":"2016-01-15T15:42:42.000Z","display_name":"Mara Cooper","language":"en","tweet_id":"688023510111916032","location":"miami","location_details":{"country":"united_states","city":"miami","state":"florida","type":"city"},"retweet_count":0,"reach":33649,"favorite_count":0,"sentiment":0,"entities":{"urls":[{"display_url":"goo.gl/fb/7hwwhZ","expanded_url":"http://goo.gl/fb/7hwwhZ","url":"https://t.co/88YCSejyyE"}]}}],"tweets_media":[{"text":"The Peach app is dead, but it taught us this about social media: https://t.co/0IJsSnb2z2 https://t.co/iqu71DKk4L","username":"NewRepublic","user_image":"http://pbs.twimg.com/profile_images/631195731756388352/rXgYnC9S_normal.jpg","timestamp":"2016-01-15T15:40:22.000Z","display_name":"New Republic","language":"en","tweet_id":"688022921747533824","location":"washington","location_details":{"country":"united_states","state":"washington","type":"state"},"retweet_count":0,"reach":139506,"favorite_count":0,"sentiment":35,"entities":{"urls":[{"display_url":"bit.ly/234xz4y","expanded_url":"http://bit.ly/234xz4y","url":"https://t.co/0IJsSnb2z2"}],"photo":[{"display_url":"pic.twitter.com/iqu71DKk4L","expanded_url":"http://twitter.com/NewRepublic/status/688022921747533824/photo/1","type":"photo","media_url":"http://pbs.twimg.com/media/CYxZM7FWMAMxrSz.jpg","resolution":{"w":1024,"h":532,"resize":"fit"},"url":"https://t.co/iqu71DKk4L"}]}},{"text":"FG will ensure social welfare for the poor – Osinbajo https://t.co/KDwyJnbAgj via #todayng https://t.co/BPpQ7GE4Gi","username":"NigeriaNewsdesk","user_image":"http://pbs.twimg.com/profile_images/559635895909752832/5m7oAlGi_normal.png","timestamp":"2016-01-15T15:39:37.000Z","display_name":"Nigeria Newsdesk","language":"en","tweet_id":"688022736917118980","location":null,"location_details":null,"retweet_count":2,"reach":1070269,"favorite_count":0,"sentiment":0,"entities":{"urls":[{"display_url":"today.ng/news/national/…","expanded_url":"https://www.today.ng/news/national/66295/fg-will-ensure-social-welfare-for-the-poor-osinbajo?utm_source=dlvr.it_nnd&utm_medium=twitter","url":"https://t.co/KDwyJnbAgj"}],"photo":[{"display_url":"pic.twitter.com/BPpQ7GE4Gi","expanded_url":"http://twitter.com/NigeriaNewsdesk/status/688022736917118980/photo/1","type":"photo","media_url":"http://pbs.twimg.com/media/CYxZCKsWYAI0NZw.jpg","resolution":{"w":1024,"h":806,"resize":"fit"},"url":"https://t.co/BPpQ7GE4Gi"}]}},{"text":"Building your personal brand on social media is as much about listening as talking. https://t.co/U8VwyRYgD6 https://t.co/1qes1tfVMB","username":"FT","user_image":"http://pbs.twimg.com/profile_images/466972537704824832/eflEColL_normal.png","timestamp":"2016-01-15T15:09:50.000Z","display_name":"Financial Times","language":"en","tweet_id":"688015237958774784","location":null,"location_details":null,"retweet_count":0,"reach":2190381,"favorite_count":0,"sentiment":35,"entities":{"urls":[{"display_url":"on.ft.com/234vi9l","expanded_url":"http://on.ft.com/234vi9l","url":"https://t.co/U8VwyRYgD6"}],"photo":[{"display_url":"pic.twitter.com/1qes1tfVMB","expanded_url":"http://twitter.com/FT/status/688015237958774784/photo/1","type":"photo","media_url":"http://pbs.twimg.com/media/CYxSNpKWMAArwFh.jpg","resolution":{"w":600,"h":338,"resize":"fit"},"url":"https://t.co/1qes1tfVMB"}]}},{"text":"Who has decided that women cannot go in?: Ranjana Kumari, Director, Centre for Social Research on #SabrimalaRow #LRC_NDTV","username":"ndtv","user_image":"http://pbs.twimg.com/profile_images/570440108424171520/QuGYd7jH_normal.png","timestamp":"2016-01-15T14:46:17.000Z","display_name":"NDTV","language":"en","tweet_id":"688009314565550080","location":"india","location_details":{"country":"india","type":"country"},"retweet_count":15,"reach":5824509,"favorite_count":6,"sentiment":0,"entities":{}}],"topUsers":[["FreeformTV",678,"100%"],["AsMickymouse82",141,"-100%"],["ILoveMyDinoMatt",120,"0%"],["CloudataNow",79,"12%"],["TheSocialMs",79,"100%"],["mhsowrove",75,"100%"],["TSM_B2B",70,"100%"],["ExploreWeb2dot0",61,"-4%"],["Go2WebMarketing",61,"-4%"],["dreckbaerfrau",58,"100%"]],"locations":[["london",523],["new_york",321],["chicago",238],["los_angeles",204],["toronto",156],["kota",152],["atlanta",129],["miami",121],["mumbai",106],["boston",104]],"time_series_mentions":{"14/01/2016_15:43":1501,"14/01/2016_16:43":1592,"14/01/2016_17:43":1692,"14/01/2016_18:43":1477,"14/01/2016_19:43":1537,"14/01/2016_20:43":1864,"14/01/2016_21:43":1313,"14/01/2016_22:43":1448,"14/01/2016_23:43":1498,"15/01/2016_00:43":1289,"15/01/2016_01:43":1094,"15/01/2016_02:43":1313,"15/01/2016_03:43":1337,"15/01/2016_04:43":1075,"15/01/2016_05:43":1069,"15/01/2016_06:43":1053,"15/01/2016_07:43":808,"15/01/2016_08:43":844,"15/01/2016_09:43":524,"15/01/2016_10:43":973,"15/01/2016_11:43":1148,"15/01/2016_12:43":1313,"15/01/2016_13:43":1272,"15/01/2016_14:43":1277},"time_series_impressions":{"14/01/2016_15:43":9631791,"14/01/2016_16:43":24597649,"14/01/2016_17:43":42674232,"14/01/2016_18:43":61093552,"14/01/2016_19:43":70688408,"14/01/2016_20:43":47669000,"14/01/2016_21:43":24028898,"14/01/2016_22:43":76327602,"14/01/2016_23:43":50294599,"15/01/2016_00:43":74220157,"15/01/2016_01:43":45603520,"15/01/2016_02:43":29450814,"15/01/2016_03:43":46223938,"15/01/2016_04:43":15439446,"15/01/2016_05:43":19868983,"15/01/2016_06:43":82828364,"15/01/2016_07:43":98291003,"15/01/2016_08:43":11781541,"15/01/2016_09:43":8872385,"15/01/2016_10:43":4314408,"15/01/2016_11:43":14093326,"15/01/2016_12:43":10846241,"15/01/2016_13:43":8302175,"15/01/2016_14:43":18581107},"time_series_sentiment":{"14/01/2016_15:43":60,"14/01/2016_16:43":67,"14/01/2016_17:43":55,"14/01/2016_18:43":69,"14/01/2016_19:43":69,"14/01/2016_20:43":61,"14/01/2016_21:43":61,"14/01/2016_22:43":58,"14/01/2016_23:43":54,"15/01/2016_00:43":61,"15/01/2016_01:43":68,"15/01/2016_02:43":60,"15/01/2016_03:43":59,"15/01/2016_04:43":57,"15/01/2016_05:43":50,"15/01/2016_06:43":70,"15/01/2016_07:43":70,"15/01/2016_08:43":61,"15/01/2016_09:43":73,"15/01/2016_10:43":66,"15/01/2016_11:43":68,"15/01/2016_12:43":70,"15/01/2016_13:43":66,"15/01/2016_14:43":67},"relatedTopics":[["#Waze",1745,-76.9],["#Freeformtv",1229,100],["#Freeformlaunch",1215,100],["#Gopdebate",693,-14.1],["#Social",668,45.2],["#Arsenal",493,-99.2],["#Elnennym",490,-100],["#Socialmedia",364,66.4],["#Marketing",261,62.2],["#Realdonaldtrump",236,69],["#Gurmeetramrahim",223,100]],"media":{"mentions":145,"impressions":230059822,"sentiment":"20%"},"people":{"mentions":30311,"impressions":895723139,"sentiment":"25.2%"}}';
I know this is avalid JSON (check here)
Now when I try to pass it to encodeURIComponent() to make it URL Safe, it doesn't encode it properly. I don't know why is it happening.
I want to pass this JSON to a PHP file from which i want to genrate a Excel file (genrated using this JSON data) using PHPExcel. I am doing this so that I can redirect the user to the php page passing this JSON as a GET parameter and which will download the file (as default behaviour of PHPExcel is it stores the file in filesystem) directly to the client without storing it to the filesystem.
By the way is it doable using AJAX, making an AJAX call to the PHP file passing this JSON as a POST data and, the PHP file builds EXCEL and respond with the excel file without uploading it to the server so that it can be directly downloaded to the client.

Your string is 8951 long before encoding, read this post to see why your GET query fails. Change your Ajax call to POST and that will work.
// x is not a string anymore, but a JSON object
var x= {"time":"Friday, January 15, 2016 9:13 PM", .... } ;
$.post ('myurl.php', x, function (ret, status) { ... }) ;

Related

get only specific element of a JSON database stored in server by url request

my website relies on a database which is a big JSON file like this:
var myjsonData =
[ {
"ID": 0,
"name": "Henry",
"surname": "McLarry",
"...": "...",
}]
I do generate this data every month at high cost to me, therefore I would like to avoid calling it straight in my html <head>, because this will allow any user to download the full database in no time.
I would like to build a "something" that can only call specific items from the json file (just the only one I want to show) without "exposing" the full .json onto client side.
today I use the call
var myvar= myjsonData.ID.Name
to get "Henry" into myvar, I would like to build something like
var myvar = mycallfunction(ID,Name)
I did try with PHP as intermediary but the ajax calls from javacript doesn't allow me to fetch the data.
Can I use JQuery with the JSON Url to get only the item I need?
What you can do is parse your json for an object. So you can get any value you want from json.
Example:
var myjsonData = '{"ID": 0,"name": "Henry","surname": "McLarry"}';
obj = JSON.parse(myjsonData);
console.log(myjsonData.ID); //print the id
console.log(myjsonData.name); //print the name
console.log(myjsonData.surname); //print the surname
So you have a NoSQL Database which has only one kind of Document that is the full JSON element you use in your website. In that scenario you have three options:
Depending on the NoSQL Database you're using you can limit the fields which will be returned(I.e: For MongoDB you can look here: https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/)
Change the way you store you data into more modular documents and make the logic to connect them in you application. So instead of one big document you'll have modular ones as Users, Products, Transactions and etc and you can use your application to query them individually.
Build a Server Side logic as an API to deal with your data and provide only what you need, so the API(Which can be node.js, php, or any you may like) will get the full JSON it`s endpoints will only the data you want. For example: myapi.com/getUser, myapi.com/getProducts and so on.
If you're able to provide more info on the technologies you're using that would help us. Hope that helped :).

Scrape CSV data to SQL table using PHP, LOAD DATA from URL with Javascript object, (beginner)

(I'm learning and not a pro, so please excuse any faux pas)
I am aware that the PHP, LOAD DATA function may be what I need, but I can't get to the file behind the Java Object.
I am trying to update a SQL table, i.e. overwrite matching dates, with data from a CSV file from Javascript buttons on one of these websites:
-"Download Data" from https://www.investing.com/rates-bonds/us-10-yr-t-note-historical-data (adjusting dates would be ideal)
-"Download Range" from https://www.barchart.com/futures/quotes/Znh18/price-history/historical
-"Download Spreadsheet" from http://quotes.wsj.com/bond/BX/TMUBMUSD10Y/historical-prices
The data looks something like this
Time,Open,High,Low,"Last Price",Change,Volume,"Open Interest"
02/08/18,121.0781,121.25,120.5313,120.8906,-0.10939999999999,2938115,0
02/07/18,121.2031,121.6094,120.8594,121,-0.48439999999999,2201308,3569670
I have used the http://simplehtmldom.sourceforge.net/ utility to extract individual pieces of data, but that seems laborious if a file exists.
e.g. currently this is my code for finding table data on the WSJ page using the file_get_html() function:
...
// Finds the last cash price
foreach($html->find('span[id=price_quote_val]') as $e){
echo str_replace("/32.","",str_replace(" ",".",$e->plaintext)). ' last cash price<br>';
$field='Last';
$value=str_replace("/32.","",str_replace(" ",".",$e->plaintext));
include('table_update.php');
}
....
Thanks!
Chris
I have read other posts, including:
Save CSV files into mysql database
Importing CSV data using PHP/MySQL

Use .ASPX file as a Server-Side code to be called via AJAX by HTML/Javascript Client

There are tools that are used by several thousand employees at my job. They are currently stored on SharePoint 2010 (to be upgraded to 2016 very soon) in a document library. Essentially, we're using SP2010 as a web server so we didn't have to charge a huge expense to our business unit for our own web server.
These tools are all based in HTML and JavaScript. However, I'm trying to automate the collection of data points that the user is currently inputting manually. To do this, I found that the best way would be to query the LDAP which is always going to be Server-Side code since there is no way to query the LDAP via the browser. Below, you'll see the JavaScript that I have and the ASPX file that I have (minus the header that sharepoint automatically creates). I'm sending an AJAX call over to the ASPX file with some data in the URL that will be needed to get the remaining data from the LDAP. However, what I'm getting back seems to be literally ALL of the code that is being stored by the browser versus the ONE JSON variable that I want.
First the JavaScript:
var ldapUserName;
var employee = {};
var ADSystem = new ActiveXObject("ADSystemInfo");
ldapUserName = ADSystem.UserName;
$.ajax({
type: "GET",
url: "http://my.url.where/i/am/storing/getEmp.aspx?username=" + ldapUserName,
success: function(data) {
employee = data;
alert(employee);
},
error: function() {
alert("Connection Failed");
}
});
Next, the ASPX file (minus the SharePoint header that's automatically added)
<%
Option Explicit
Response.LCID = 1043
%>
<!--#include file="jsonObject.class.aspx" -->
<%
Dim username
username = Request.QueryString("username")
empDemo(username)
Public Function empDemo(username)
'Create the Array that will be passed
Set JSON = New JSONobject
'Employee specific information
Set objUser = GetObject("LDAP://" & username)
JSON.Add "empNum", objUser.sAMAccountName
JSON.Add "fName", objUser.givenName
JSON.Add "lName", objUser.sn
JSON.Add "fullName", objUser.displayName
JSON.Add "ext", objUser.telephoneNumber
JSON.Add "title", objUser.title
JSON.Write()
End Function
%>
Notice that I'm pulling in a file which helps the VBScript work with JSON variables. That file can be found here.
Again, what getting back seems to be quite literally all of the source code of the ASPX file. What I need is to only get back the JSON variable. Anyone have any suggestions?
Your server does not know what an "aspx" file is. It thinks it is a text file and so its just returning the text of your file.
I am not sure how you would fix this as I am not familiar with Sharepoint. As Sharepoint is a document management system, It appears sharepoint is doing what it was built for. Storing documents and then allowing people to download them. There appears to be no interpreter on the Sharepoint servers for "aspx" files.
This URL might provide help in enabling this if it is even possible: https://support.microsoft.com/en-us/help/828810/how-to-enable-an-asp.net-application-to-run-on-a-sharepoint-virtual-server

Using Javascript to make parallel server requests THREDDS OPeNDAP

For the following THREDDS OPeNDAP server:
http://data.nodc.noaa.gov/thredds/catalog/ghrsst/L2P/MODIS_T/JPL/2015/294/catalog.html
I would like to note four Attributes of every file in there. The attributes are:
northernmost lattitude; easternmost lattitude; westernmost lattitude; southernmost lattitude. These can be found under the Global attributes under:
http://data.nodc.noaa.gov/thredds/dodsC/ghrsst/L2P/MODIS_T/JPL/2015/294/20151021-MODIS_T-JPL-L2P-T2015294235500.L2_LAC_GHRSST_N-v01.nc.bz2.html
At first I tried this with MATLAB. Problem is: all the netcdf files on the server are compiled to .bz2 files. This makes calling for the Global attributes take around 15 seconds (the server is extracting the file). I would like javascript to run these server requests parallel to save me time. In total I need 90,000 files.
Is there a way to code this using javascript?
You can use the THREDDS DAS service. DAS
Change the OPenDAP link you have above replacing the .html extension with .das
This is a small text file with metadata about the file which could be easily parsed with javascript and includes a section with the global attributes:
NC_GLOBAL {
. . .
Float32 northernmost_latitude 89.9942;
Float32 southernmost_latitude 66.9853;
Float32 easternmost_longitude -121.445;
Float32 westernmost_longitude 76.7485;
. . .
}
This metadata is cached by THREDDS and the above DAS link responds instantly.
Edit:
Re: the correct comments below, (cache exists only after the first request) one alternative might be to use the source data at the NASA JPL OPeNDAP Server (Hyrax): http://podaac-opendap.jpl.nasa.gov/opendap/allData/ghrsst/data/L2P/MODIS_T/JPL/
My browser only tests (i.e. subjective) seem to show that a random DAS responses are quicker, than 15 seconds.
http://podaac-opendap.jpl.nasa.gov/opendap/allData/ghrsst/data/L2P/MODIS_T/JPL/2015/294/20151021-MODIS_T-JPL-L2P-T2015294084500.L2_LAC_GHRSST_N-v01.nc.bz2.das

Local HTML 5 database usable in Mac Dashboard wigdets?

I'm trying to use HTML 5's local database feature on a Mac Dashboard widget.
I'm programming in Dashcode the following javascript:
if (window.openDatabase)
{
database = openDatabase("MyDB", "1.0", "Sample DB", 1000);
if (database)
{
...database code here...
}
}
Unfortunately the database-variable remains always null after the call to openDatabase-method. I'm starting to think that local databases are not supported in Widgets...
Any ideas?
/pom
No you will not be able to do the above. And even if you could then you would not be able to distribute the widget without distributing the database assuming it was a MySQL or SGLite. (not sure what you mean by HTML 5's local Db.
here are a number of ways round this:-
You can add a data source which can be a JSON file, or an XML file or and RSS feed. So to do this with JSON for example you would write a page on a server in PHP or something that accessed a database so that when the URL was called the result was a JSON string. Take the JSON string and parse it and use it in the Widget. This will let you get data but not save it.
Another way would be to use the user preferences. This allows you to save and retrieve data in the individual widget.
So
var preferenceKey = "key"; // replace with the key for a preference
var preferenceValue = "value"; // replace with a preference to save
// Preference code
widget.setPreferenceForKey(preferenceValue, preferenceKey);
You can then retrieve it with
var preferenceForKey = "key"; // replace with the key for a preference
// Preference code
preferenceForKey = widget.preferenceForKey(preferenceForKey);
The external call, you could also use REST will let you read any amount of data in and the preferences will let you save data for later reuse that will survive log out's and shut downs.
The Apple site has a lot of information about Widgets and tutorials as well thjat are worth working through.
Hope this helps.

Categories