Export data to JSON file

Posted about 5 years ago by Antonio Poggi

Post a topic
Un Answered
Antonio Poggi
Antonio Poggi Admin

Here is described a powerful and easy way to export your project blocks data to a JSON file using the Python block built-in capabilities as a Output block.


You simply need to have a Python block with a 1 line script to instruct Omniscope to transform the input block data into a JSON file and output it to your disk.


input_data.to_json('/home/user/files/myFile.json', orient='records')


I have created a project to demonstrate it : https://omniscope.me/Forums/Export+data+to+JSON+file.iox/ 


We can use the Python to_json method to write data in JSON format and use the orient parameter to control the data structure.


Say that your block data is a table made of 2 columns and 2 rows:


Col 1 Col 2
Row 1ab
Row 2cd


A) Writing using 'records' formatted JSON. 
input_data.to_json('/home/youruser/omniscope-server/files/myFile.json', orient='records')

output

'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'


B) Writing using 'index' formatted JSON
input_data.to_json('/home/youruser/omniscope-server/files/myFile.json', orient='index')

output:

'{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}'


C) Writing using 'columns' formatted JSON
input_data.to_json('/home/youruser/omniscope-server/files/myFile.json', orient='columns')

output:

'{"col 1":{"row 1":"a","row 2":"c"},"col 2":{"row 1":"b","row 2":"d"}}'


D) Writing using 'values' formatted JSON
input_data.to_json('/home/youruser/omniscope-server/files/myFile.json', orient='values')

output:

'[["a","b"],["c","d"]]'


E) Writing with Table Schema
input_data.to_json('/home/youruser/omniscope-server/files/myFile.json', orient='table')

output:

'{"schema": {"fields": [{"name": "index", "type": "string"},
                        {"name": "col 1", "type": "string"},
                        {"name": "col 2", "type": "string"}],
             "primaryKey": "index",
             "pandas_version": "0.20.0"},
  "data": [{"index": "row 1", "col 1": "a", "col 2": "b"},
           {"index": "row 2", "col 1": "c", "col 2": "d"}]}'

0 Votes


0 Comments

Login or Sign up to post a comment