0

I am using the golang library go-jmespath to query a json file. My code looks like the following:

package main

import (
    "encoding/json"
    "log"

    "github.com/jmespath/go-jmespath"
)

func main() {
    var jsonBlob = []byte(`[
    {
        "oeeId": 3162396,
        "oeeDate": "2019-03-06T00:00:00",
        "oee": 21.2
    }]`)

    var d interface{}

    err := json.Unmarshal(jsonBlob, &d)
    if err != nil {
        log.Printf("1: %s", err)
    }

    res, err := jmespath.Search("{ labels: ['example'], datasets: [{label: 'Verfügbarkeit', data: [?oeeDate == `2019-03-06T00:00:00`].oee}]}", d)
    if err != nil {
        log.Printf("2: %s", err)
    }
    log.Println(res)
}

Here is also a link to an example in the Playground.

When I execute this code I get the following error:

invalid character '-' after top-level value

I am wondering what's my issue with this code since this example works with other Jmespath implementations like the javascript jmespath.js library.

The error seems to be in the query rather than the input data. Can anybody help me on this one?

thiloilg
  • 1,733
  • 2
  • 18
  • 23
  • 1
    The code in question does not produce the given error message, try it on the [Go Playground](https://play.golang.org/p/n_YRmX29eep). Please provide a [mcve]. – icza Jan 29 '20 at 13:08
  • 1
    @thiloilg according to the [spec](http://jmespath.org/specification.html) backticks `\`` are used for json literals and as far as i know `2019-03-06T00:00:00` is not a json literal, try `'2019-03-06T00:00:00'` or `\`'2019-03-06T00:00:00'\``. – mkopriva Jan 29 '20 at 13:56
  • @mkopriva I understand. So the jmespath.js library probably casted it to a string which the go-jmespath library doesn't. – thiloilg Jan 29 '20 at 14:06

1 Answers1

1

Replacing the backticks in the search string with single quote removes the error.

Use this instead: "{ labels: ['example'], datasets: [{label: 'Verfügbarkeit', data: [?oeeDate == '2019-03-06T00:00:00'].oee}]}".

No more error.

chmike
  • 20,922
  • 21
  • 83
  • 106