PINE LIBRARY
Cập nhật

TO_JSON

411
TO_JSON

A lightweight Pine Script v6 library for converting TradingView series data into JSON-formatted alert payloads.

`TO_JSON` is designed for users who want to export chart data to external systems through TradingView alerts.
It helps transform rolling series such as OHLCV, indicator values, or text states into JSON-compatible arrays and wraps them into a structured message for webhooks, bots, dashboards, or automated workflows.

What this library does

This library provides helper functions to:

- convert float series into JSON arrays
- convert integer series into JSON arrays using a sentinel value as `null`
- convert string series into JSON arrays with fallback filling and JSON-safe escaping
- wrap custom payloads into a top-level JSON alert message with symbol, timeframe, timestamp, and token

It is especially useful when you want to send the latest `N` bars of market data or indicator values to an external service.

Features

- Rolling array export for `float`, `int`, and `string` series
- Chronological output from oldest to newest
- `null` support for missing values
- JSON-safe string escaping
- Simple wrapper for alert message generation
- Works well for webhook-based automation and downstream parsing

Exported functions

### `series_to_array_float_null(series float src, int N)`

Converts the latest `N` values of a float series into a JSON array string.
Missing values are exported as `null`.

`series_to_array_int_sentinel(series int src, int N, int sentinel)`

Converts the latest `N` values of an integer series into a JSON array string.
The specified sentinel value is exported as `null`.

`series_to_array_str_fill(series string src, int N, string fill="")`

Converts the latest `N` values of a string series into a JSON array string.
Missing values are replaced with `fill`, and strings are escaped for JSON compatibility.

`json(simple string token="1234567890", string info="", simple string symbol="AUTO")`

Wraps a custom JSON payload into a top-level alert message including:

- symbol
- timeframe
- current timestamp
- human-readable time
- info payload
- token

Typical use case

A common use case is exporting the last bars of:

- time
- open
- high
- low
- close
- volume
- moving averages
- custom signals

into one flat JSON object, then embedding it into the alert message.

Example

```pine
//version=6
import veegee82/TO_JSON/1 as json

indicator("TO_JSON Example", overlay=false)

stO = open
stH = high
stL = low
stC = close
stV = volume

ema_50 = ta.ema(close, 50)
ema_100 = ta.ema(close, 100)
ema_200 = ta.ema(close, 200)
ema_500 = ta.ema(close, 500)
ema_1000 = ta.ema(close, 1000)

json_flat(int n=50) =>
s_name = '"name":"' + 'vision_' + timeframe.period + '"'
s_tf = ',"timeframe":"' + timeframe.period + '"'
s_ts = ',"ts":' + json.series_to_array_float_null(time, n)

s_o = ',"open":' + json.series_to_array_float_null(stO, n)
s_h = ',"high":' + json.series_to_array_float_null(stH, n)
s_l = ',"low":' + json.series_to_array_float_null(stL, n)
s_c = ',"close":' + json.series_to_array_float_null(stC, n)
s_v = ',"volume":' + json.series_to_array_float_null(stV, n)

s_ema_50 = ',"ema_50":' + json.series_to_array_float_null(ema_50, n)
s_ema_100 = ',"ema_100":' + json.series_to_array_float_null(ema_100, n)
s_ema_200 = ',"ema_200":' + json.series_to_array_float_null(ema_200, n)
s_ema_500 = ',"ema_500":' + json.series_to_array_float_null(ema_500, n)
s_ema_1000 = ',"ema_1000":' + json.series_to_array_float_null(ema_1000, n)

"{" + s_name + s_tf + s_ts + s_o + s_h + s_l + s_c + s_v + s_ema_50 + s_ema_100 + s_ema_200 + s_ema_500 + s_ema_1000 + "}"

info = json_flat(100)

alert(
message = json.json(token = "1234567890", info = info, symbol = "AUTO"),
freq = alert.freq_once_per_bar_close
)
Phát hành các Ghi chú
v2
fix token

Thông báo miễn trừ trách nhiệm

Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.