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
myJson = '''[
{"Column 1": 1,"API":"42013342780000","PRODUCTION":[{"MONTH": "2011-03-01","OIL_BOE":5657,"GAS_BOE":1},{"MONTH":"2011-04-01","OIL_BOE":6328,"GAS_BOE":1}]},
{"Column 1": 2,"API":"42013309790000","PRODUCTION":[{"MONTH": "2002-01-01","OIL_BOE": 3,"GAS_BOE":1}, {"MONTH": "2002-02-01","OIL_BOE": 4,"GAS_BOE":1}]},
{"Column 1": 3,"API":"42013309800000","PRODUCTION":[{"MONTH": "2002-01-01","OIL_BOE": 5,"GAS_BOE":1}, {"MONTH": "2002-02-01","OIL_BOE": 6,"GAS_BOE":1}, {"MONTH": "2002-03-01","OIL_BOE": 7,"GAS_BOE":1}]}
]'''
# Deserialize the JSON to a .net object
flowers = JavaScriptSerializer().DeserializeObject(myJson)
strData = ''
for f
in flowers:
for x
in f['PRODUCTION']:
strData += f['API'] + '\t' + str(x['MONTH']) + '\t' + str(x['OIL_BOE'])+ '\t' + str(x['GAS_BOE']) + '\r\n'
def generateDataTableFromString(strData, tblName, colHeaders, colTypes):
# headers
headers = '\t'.join(colHeaders) + '\r\n'
# append data to header
strData = headers + strData
# make a stream from the string
stream = MemoryStream()
writer = StreamWriter(stream)
writer.Write(strData)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)
# set up the text data reader
readerSettings = TextDataReaderSettings()
readerSettings.Separator = "\t"
readerSettings.AddColumnNameRow(0)
# assign data type
for i, col
in enumerate(colTypes):
readerSettings.SetDataType(i, col)
# 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(tblName):
Document.Data.Tables[tblName].ReplaceData(textDataSource)
else:
newTable = Document.Data.Tables.Add(tblName, textDataSource)
tableSettings = DataTableSaveSettings(newTable, False, False)
Document.Data.SaveSettings.DataTableSettings.Add(tableSettings)
# get parameters to build route details tables
tblName = 'well_data_1'
tblColumnsHeaders = ['API', 'MONTH','OIL_BOE','GAS_BOE']
tblColumnsTypes = [DataType.String , DataType.Date, DataType.Real , DataType.Real]
# generate tables
generateDataTableFromString(strData, tblName, tblColumnsHeaders, tblColumnsTypes)