as_json: Convert a result to JSON

View source: R/json.R

as_jsonR Documentation

Convert a result to JSON

Description

The whole result as JSON, under a documented and versioned schema, for software that consumes changepoint results rather than a person reading them.

Usage

as_json(x, ...)

## S3 method for class 'ggcpt'
as_json(x, pretty = TRUE, data = TRUE, assumptions = TRUE, digits = NA, ...)

## Default S3 method:
as_json(x, ...)

Arguments

x

A ggcpt object.

...

Ignored.

pretty

Indent the output? Defaults to TRUE.

data

Include the series itself (index, value and, where the engine supplied one, fitted)? Defaults to TRUE; without it the output is the answer alone, a few hundred bytes.

assumptions

Include cpt_assumptions()? Defaults to TRUE.

digits

Significant digits for numbers. Defaults to NA, the full precision needed for an exact round trip.

Value

A single string of class "json".

Schema 1.0.0

One object with these fields. Every field is always present; one that does not apply is null, so a reader never has to test for a key.

schema, schema_version

"ggchangepoint.ggcpt" and the version, "1.0.0".

method, engine, engine_version, ggchangepoint_version, r_version, created

what produced the result, and when (ISO 8601).

change_in, family

what was detected, and the distribution family if one was asked for.

penalty

{"type", "value"}.

cp_convention

"left": a changepoint is the last observation of its segment.

n

the series length.

index

{"class", "label", "tz"} of the time index, or null. Dates are written as ISO 8601 strings and timestamps with their offset; tz is a timestamp index's time zone (null otherwise).

changepoints

an array of objects, one per changepoint: cp (position) and cp_value, with cp_index when there is an index, and every engine-specific column (ci_lower, ci_upper, posterior_prob, ...).

segments

seg_id, start, end, n, param_estimate.

regions, coefficients, constraints

the optional slots, as arrays of objects (or an object), or null.

diagnostics

residual_dependence (Ljung-Box p_value, lag, acf1), na_omitted and expected_false_positives where recorded.

assumptions

the cpt_assumptions() rows, or null.

call

the call that made the result, as text.

data

{"index", "value", "fitted", "coordinates"} arrays, or null. coordinates is an object with one array per coordinate of a multivariate result (null for a single series), whose first is value.

See Also

cpt_export() to write a file, cpt_import() to read one back, cpt_report(format = "json").

Other result class: annotate_segments(), as_cpt_series(), as_ggcpt(), cpt_annotations(), cpt_export(), cpt_verify(), ggchangepoint-conditions, is_ggcpt(), new_ggcpt(), print.ggcpt()

Examples


set.seed(1)
fit <- cpt_detect(c(rnorm(50), rnorm(50, 3)), method = "pelt")
cat(as_json(fit, data = FALSE))


ggchangepoint documentation built on Nov. 5, 2026, 9:06 a.m.