Documentation

Using reduce() to construct a JSON.

Send data in JSON body with http.post()

Use the reduce() function to create a JSON object to include as the body with http.post().

  1. Import both the array package to query data and construct table(s), and the http package to transfer JSON over http.

  2. Use array.from() to query data and construct a table. Or, use another method to query data with Flux.

  3. Use the reduce() function to construct a JSON object, and then use yield() to store the output of reduce. This table looks like:

    field tag
    example-field:[“3"4"1 {example-tag-key:[“bar"bar"bar
  4. Use the map() function to combine the two components together into a JSON object, and then use a second yield() function to store this object as final JSON. This table looks like:

    field tag final
    example-field:[“3"4"1 {example-tag-key:[“bar"bar"bar {example-tag-key:[“bar"bar"bar] , example-field:[“3"4"1]}
  5. Use the findRecord() function to extract the value from the final column, the JSON.

  6. Use http.post() to specify a URL to sent the JSON to. In this example, we use Post Test Server as URL to send the JSON to, and test the http.post() function.

import "array"
import "http"

 
data = array.from(
        rows: [
            {_time: 2020-01-01T00:00:00Z, _field: "example-field", _value: 3, foo: "bar"},
            {_time: 2020-01-01T00:01:00Z, _field: "example-field", _value: 4, foo: "bar"},
            {_time: 2020-01-01T00:02:00Z, _field: "example-field", _value: 1, foo: "bar"},
        ],
    )
  
    |> reduce(
            fn: (r, accumulator) => ({tag:accumulator.tag + "\"" + r.foo, 
                                    field : accumulator.field + "\"" + string(v:r._value)
                                    }),
            identity: {tag: "{example-tag-key:[", 
                    field: "example-field:[" }
    )
    |> yield(name: "output of reduce")
    |> map(fn: (r) => ({ r with tag: r.tag + "]" }))
    |> map(fn: (r) => ({ r with field: r.field + "]}" }))
    |> map(fn: (r) => ({ r with final: r.tag + " , " + r.field}))
    |> yield(name: "final JSON")
    |> findRecord(
        fn: (key) => true,
        idx: 0,
        )


http.post(
    url: "https://ptsv2.com/t/c4x38-1656014222/post",
    headers: {"Content-type": "application/json"},
    data: bytes(v: data.final),
    )

Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB v3 enhancements and InfluxDB Clustered is now generally available

New capabilities, including faster query performance and management tooling advance the InfluxDB v3 product line. InfluxDB Clustered is now generally available.

InfluxDB v3 performance and features

The InfluxDB v3 product line has seen significant enhancements in query performance and has made new management tooling available. These enhancements include an operational dashboard to monitor the health of your InfluxDB cluster, single sign-on (SSO) support in InfluxDB Cloud Dedicated, and new management APIs for tokens and databases.

Learn about the new v3 enhancements


InfluxDB Clustered general availability

InfluxDB Clustered is now generally available and gives you the power of InfluxDB v3 in your self-managed stack.

Talk to us about InfluxDB Clustered