How to format JSON response

  • What Grafana version and what operating system are you using?
    Grafana v11.0.1 (302831c125) OSS

  • What are you trying to achieve?
    I am trying trying to create a line graph with the Infinity datasource

  • How are you trying to achieve it?
    I have an API that returns the following JSON

{
  "timeseries": [
    {
      "label": "A",
      "time": "2024-04-01",
      "value": 270
    },
    {
      "label": "B",
      "time": "2024-04-01",
      "value": 446
    },
    {
      "label": "A",
      "time": "2024-04-02",
      "value": 382
    },
    {
      "label": "B",
      "time": "2024-04-02",
      "value": 146
    },
  • What happened?
    Its not splitting the data into two lines

  • What did you expect to happen?
    I expected the visualization to show two lines

  • Can you copy/paste the configuration(s) that you are having problems with?
    Type: JSON
    Parser: Default
    Source: URL
    Format: Table

I would appreciate any help

One of possible solutions is to use two Grafana transformations:

1 - Grouping to Matrix

2 - Convert field type

Result:

Thank you very much for taking the time to look at my issue and provide me a solution. I appreciate it.

Is there any way I can structure the JSON reponse so that I dont have to use transformations? Thank you again.

Try something like this:

{
    "timeseries": [
        {
            "time": "2024-04-01T00:00:00Z",
            "A": "270.000000",
            "B": "446.000000"
        },
        {
            "time": "2024-04-02T00:00:00Z",
            "A": "382.000000",
            "B": "146.000000"
        }
    ]
}

That is awesome and exactly what I wanted. Thank you again very much for the quick and concise solution.

you dont need to restructure it nor use transformations

are you using uql by any chance to parse the json?
something like

I am using Inline source because I will be using the sample json you posted. Type: UQL, Format: Time Series

parse-json
| scope "timeseries"
| pivot sum("value"),  "time", "label"

image

Thank you for this. I will try it.