Loading Json Web API Open Source Data into Spotfire Client
Do You want to perform analysis on open source yahoo financial data which can be accessed by JSON ? So how do you achieve this?
So let’s get started.
- We can achieve this by either using Iron Phyton Script .
- Or we can achieve this by using CIS(Cisco Composite Studio) a Data Virtualization service.
In this post, we will load the JSON data by using Iron Phyton Script.
Step 1:
Create a document Property called “Ticker Symbol” & enter any ticker symbol value.
Step 2:
import clr
clr.AddReference(‘System.Data’)
clr.AddReference(‘System.Web.Extensions’)
import System
from System import DateTime
from System.Data import DataSet, DataTable
from System.IO import StreamReader, StreamWriter, MemoryStream, SeekOrigin
from System.Net import HttpWebRequest
from System.Web.Script.Serialization import JavaScriptSerializer
from Spotfire.Dxp.Data import DataType, DataTableSaveSettings
from Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings
# get stock quotes data in JSON format from Yahoo Finance API
tickerSymbol = Document.Properties[“TickerSymbol”]
startDate = DateTime.Today.AddMonths(-3).ToString(“yyyy-MM-dd”)
endDate = DateTime.Today.ToString(“yyyy-MM-dd”)
uri = “http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.historicaldata where symbol = %22” + tickerSymbol + “%22 and startDate = %22” + startDate + “%22 and endDate = %22” + endDate + “%22&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys”
webRequest = HttpWebRequest.Create(uri)
response = webRequest.GetResponse()
streamReader = StreamReader(response.GetResponseStream())
jsonData = streamReader.ReadToEnd()
js = JavaScriptSerializer()
dataDict = js.Deserialize(jsonData,object)
# build a string representing the data in comma-delimited text format
textData = “Symbol,Date,Close\r\n”
for quote in dataDict[“query”][“results”][“quote”]:
textData += tickerSymbol + “,” + quote[“Date”] + “,” + quote[“Close”] + “\r\n”
# make a stream from the string
stream = MemoryStream()
writer = StreamWriter(stream)
writer.Write(textData)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)
# set up the text data reader
readerSettings = TextDataReaderSettings()
readerSettings.Separator = “,”
readerSettings.AddColumnNameRow(0)
readerSettings.SetDataType(0, DataType.String)
readerSettings.SetDataType(1, DataType.Date)
readerSettings.SetDataType(2, DataType.Currency)
# create a data source to read in the stream
textDataSource = TextFileDataSource(stream, readerSettings)
# add the data into a Data Table in Spotfire
if Document.Data.Tables.Contains(“Stock Data”):
Document.Data.Tables[“Stock Data”].ReplaceData(textDataSource)
else:
newTable = Document.Data.Tables.Add(“Stock Data”, textDataSource)
tableSettings = DataTableSaveSettings (newTable, False, False)
Document.Data.SaveSettings.DataTableSettings.Add(tableSettings)
There is where you will write your script.
Note: The above code has been tested 6.5,7.0 Version Spotfire.
Error:
File “”, line 28
textData + = tickerSymbol + quote[“Date”] + quote[“Close”] + “\r\n”
^
SyntaxError: expected an indented block
This is not working in Spotfire Webplayer
File “”, line 2
clr.AddReference(‘System.Data’)
^
SyntaxError: unexpected token ‘‘’
Microsoft.Scripting.SyntaxErrorException: unexpected token ‘‘’
at Microsoft.Scripting.ErrorSink.Add(SourceUnit source, String message, SourceSpan span, Int32 errorCode, Severity severity)
at IronPython.Compiler.Parser.ReportSyntaxError(Int32 start, Int32 end, String message, Int32 errorCode)
at IronPython.Compiler.Parser.ReportSyntaxError(Token t, IndexSpan span, Int32 errorCode, Boolean allowIncomplete)
at IronPython.Compiler.Parser.ParsePrimary()
at IronPython.Compiler.Parser.ParsePower()
at IronPython.Compiler.Parser.ParseFactor()
at IronPython.Compiler.Parser.ParseExpr(Int32 precedence)
at IronPython.Compiler.Parser.ParseComparison()
at IronPython.Compiler.Parser.ParseNotTest()
at IronPython.Compiler.Parser.ParseAndTest()
at IronPython.Compiler.Parser.ParseOrTest()
at IronPython.Compiler.Parser.ParseExpression()
at IronPython.Compiler.Parser.FinishArgListOrGenExpr()
at IronPython.Compiler.Parser.AddTrailers(Expression ret, Boolean allowGeneratorExpression)
at IronPython.Compiler.Parser.ParsePower()
at IronPython.Compiler.Parser.ParseFactor()
at IronPython.Compiler.Parser.ParseExpr(Int32 precedence)
at IronPython.Compiler.Parser.ParseComparison()
at IronPython.Compiler.Parser.ParseNotTest()
at IronPython.Compiler.Parser.ParseAndTest()
at IronPython.Compiler.Parser.ParseOrTest()
at IronPython.Compiler.Parser.ParseExpression()
at IronPython.Compiler.Parser.ParseTestListAsExpr()
at IronPython.Compiler.Parser.ParseExprStmt()
at IronPython.Compiler.Parser.ParseSimpleStmt()
at IronPython.Compiler.Parser.ParseFileWorker(Boolean makeModule, Boolean returnValue)
at IronPython.Compiler.Parser.ParseFile(Boolean makeModule, Boolean returnValue)
at IronPython.Runtime.PythonContext.ParseAndBindAst(CompilerContext context)
at IronPython.Runtime.PythonContext.CompilePythonCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
at IronPython.Runtime.PythonContext.CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
at Spotfire.Dxp.Application.IronPython27.IronPythonScriptEngine.ExecuteForDebugging(String scriptCode, Dictionary`2 scope, Stream outputStream)