function::json_add_array_numeric_metric - Linux


Overview

function::json_add_array_numeric_metric inserts a numerical metric into a JSON string and appends it to an array. It’s commonly used in cloud functions and monitoring systems to build time series data and record performance metrics.

Syntax

function::json_add_array_numeric_metric(str json, str key, double value) -> str

Parameters

  • json: The JSON string to modify.
  • key: The key to set the value for.
  • value: The numerical value to set.

Options/Flags

This command does not have any options or flags.

Examples

Example 1: Adding a single numeric metric

json=$(function::json_add_array_numeric_metric '{"metrics": []}' "latency" 125)

Example 2: Adding multiple numeric metrics

json=$(function::json_add_array_numeric_metric \
    '{ "metrics": [{"latency": 125}] }' "latency" 150)

Common Issues

  • Key not found: Ensure that the specified key exists in the JSON string.
  • Invalid JSON: The input JSON must be a valid JSON string.

Integration

This command can be combined with other commands to process JSON data in Linux scripts. For example, you could use grep to filter the modified JSON based on specific criteria:

filtered_json=$(grep "latency" "$json")

Related Commands

  • function::json_add_array_string_metric
  • function::json_add_array_bool_metric
  • jq(1) – A flexible command-line JSON processor