LoginSignup
1
0

More than 1 year has passed since last update.

saveAsJSONL.js【JSON Lines】

Last updated at Posted at 2022-05-04
function saveAsJSONL(data, name=new Date().toLocaleString('sv').replace(' ','T').replaceAll(':','_')+'.jsonl') {
  const a = document.createElement('a')
  a.download = name
  a.href = URL.createObjectURL(new Blob([data.map(JSON.stringify).join('\n')], {type: 'application/jsonlines+json'}))
  a.click()
}

JavaScriptのデータを保存する

Usage

saveAsJSONL(['pen', 'pineapple', 'apple', 'pen'])

// ファイル名を指定
saveAsJSONL(['pen', 'pineapple', 'apple', 'pen'], 'hoge.jsonl')

image.png

Pythonでデータを読み込む

import pandas as pd

pd.read_json('hoge.jsonl', lines=True)

image.png

JavaScriptでデータを読み込む

const url = 'https://gist.githubusercontent.com/GitHub30/7599230721e390ec51345f7db3e930db/raw/4ebb3c648499200593e84315c362105008d01869/hoge.jsonl'
const array = await fetch(url).then(r=>r.text()).then(text => text.split('\n').map(JSON.parse))
console.log(array)

See the Pen Untitled by John Doe (@04) on CodePen.

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0