Best way to store data retrieved from JSON file? - json

I'm hoping to store data I get from a server which sends data via JSON. I don't want anything fancy - just would like to save the data so I can play with it in excel.
Here is the JSON URL: http://realm3.castle.wonderhill.com/api/map.json
I'm extremely surprised there are no solutions out there on this yet.
What would you guys used to achieve this?

Convert the json into a text file formatted as CSV - Excel can read that. I'll come up with some sample code in PHP after dinner.
EDIT: Bah, dinner can wait.
<?php
// Download data to a string
$mapData = file_get_contents('http://realm3.castle.wonderhill.com/api/map.json');
// Convert JSON into an Array
$mapData = json_decode($mapData);
var_dump($mapData);
echo "\n";
Saved the above into test.php and then ran it like this:
php test.php | less
and the output is a huge data structure. You'll need to extract what you want and then use fputcsv() to write the content to a file that you'll then read into Excel. The output doesn't seem to have any special characters, but if you do have a problem make sure to encode the data as CP1252 so Excel for Windows can read it.

Related

SAS input json into HTML

Im creating an HTML file from SAS like the following
data _null_;
file './test.html';
put '<DOCTYPE html>';
put '<html>';
put '<script>'
put '</script>'
put '</html>'
The problem is that I need to take a SAS dataset, convert it to JSON format and insert it into the HTML file. The pseudo code is:
data _null_;
file './test.html';
put '<DOCTYPE html>';
put '<html>';
put '<script>';
sasDataFrame -> to Json
put 'console.log(sasDataFrame)';
put '</script>'
put '</html>'
I know that proc JSON allows me to convert SAS dataset to Json, but i don't know how to embed the string result into an HTML through this sort of put statement.
Anyone knows how to accomplish this?
Create the JSON file as a text file using PROC JSON or whatever method you want. Then write the contents of that file into your HTML file.
So let's assume you have pointed the fileref JSON to file with the JSON text in it.
Now first write the header information, then the contents of the file, then the trailing information.
With your simple example you could do it all in one data step. Just read the lines from the JSON text file and write them to the HTML file you are creating.
data _null_;
file './test.html';
if _n_=1 then put
'<DOCTYPE html>'
/ '<html>'
/ '<script>'
;
if eof then put
'</script>'
/ '</html>'
;
infile json end=eof;
input;
put _infile_;
run;
For more complex file generation you can use multiple data steps to construct the HTML file. Just use MOD option on the FILE statement to append text to an existing file instead of making a new file.
Streaming web content like this is an antipattern!!
Rather than fiddle around with put statements, why not serve your index.html through the web server, and send the data from SAS directly as a pure JSON payload?
Here's a repo we created that will let you talk to SAS from frontend: https://github.com/sasjs/adapter
And here's another one with a ready-made web app you can use as a starting point: https://github.com/sasjs/minimal-seed-app
Whilst plugging SASjs (an open source tool my team created) I'll also point out that by using this approach you can easily deploy and run your web app on Viya and Base SAS as well as SAS 9 BI. It'll also run much faster and be easier to maintain.

JSON API Call - GET

I'm new to API's and have been spending way longer than I should have trying to get data from a website. Can someone write me a simple program? I have a text file with all the URL's that I'm trying to call and the naming format I'd like them in.
Data File with a location of C:\file.txt
Format is listed below
{URL}, {Name of file}
I want something to read the url and download the JSON data and name the file as detailed in the text file. I don't feel this is too difficult of a request but I'm getting frustrated that I can't get it. Someone please help.

Converting JSON txt file to XML

We're constructing a network of data and part of that includes modifying a search query from a public website to pull all of the data we want. That data, however, when pulled is stored into a JSON txt file.
Ultimately we want this data to be stored in an Access Database so the next step, we thought, was to convert it to XML so we can have an Excel sheet to import. We found a formatting tool (http:jsonformatter.org). When running the tool we received the following error:
“Microsoft Access has encountered an error processing the XML schema in file ‘Data.xml’,
A document must contain exactly one root element”
I've no idea what this entails or where to start debugging. Are there alternatives we might consider?
The error says that there is more than one root element. Have you validated the XML generated? I looked at the website. I tried to ask via comment but I don't have enough rep but you should post some of your json and xml.
If I am reading your issue correctly, you are converting json to xml format and then to excel?
I would suggest writing some code to consume the json and export the xml files to import.

Writing JSON data to a JSON file

Hello in my application I am currently trying to create my own custom log files in .json format. Reason for this is because I want a well structured and accurate log file which can be easily read and would not depend on some special code in my application to read the data.
I have been able to create a json file: activities.json
I have been able to write and append to that file using File::append($path, $json)
This is a sample of the file contents:
{"controller":"TestController","function":"testFunction()","model":"Test","user":"Kayla","description":"Something happened!! Saving some JSON data here!","date":"2016-06-15"}
{"controller":"TestController","function":"testFunction()","model":"Test","user":"Jason","description":"Something happened!! Saving some JSON data here!","date":"2016-06-15"}
{"controller":"UserController","function":"userFunction()","model":"User","user":"Jason","description":"Another event occurred","date":"2016-06-15"}
Now my issue is the above is not a valid JSON. How do I get it in this format:
[
{"controller":"TestController","function":"testFunction()","model":"Test","user":"Kayla","description":"Something happened!! Saving some JSON data here!","date":"2016-06-15"},
{"controller":"TestController","function":"testFunction()","model":"Test","user":"Jason","description":"Something happened!! Saving some JSON data here!","date":"2016-06-15"},
{"controller":"UserController","function":"userFunction()","model":"User","user":"Jason","description":"Another event occurred","date":"2016-06-15"}
]
Is there a way of writing and appending to a json file in laravel? As much as possible I want to avoid reading the entire file before the append and doing a search and replace since the file may contain hundreds to thousands of records.
I will not use the default Laravel Log function.

how to save JSON data into SDCard phoneGap

We have sqLite Table.Now We converted db data into JSON Object.We need JSON data save to my mobile Sd_Card.Any file like .txt or excel or word any type of format not problem.
We need JSON data Save to Local phone-memory or SD-Card.guide me how to save.First Tell me it's possible or not
Well, all you are asking is if it is possible, so that's easy, yes. You said "phone memory" or SD-Card. I assume you mean persistent memory, like with WebSQL. Since JSON is just a string, you can insert it into a WebSQL table just fine. You could also, more easier, store it in LocalStorage. Finally, if you want to use the file system, that's simple too, just be sure to add the File plugin.