Parsing inconsitent json structure

58 Views Asked by At

I am using duckling as a tool for date/time and unit extraction. When I send a request like today, I am getting (some fields have been removed for readability):

{"body":"today", "value": {"value":"2019-08-10T00:00:00.000-07:00", "grain":"day", "type":"value"}, "dim":"time"}

Thus, I created these structs:

type DucklingEntry struct {
    Body string `json:"body"`
    Dim string `json:"dim"`
    Value DucklingValue `json:"value"`
}

type DucklingValue struct {
    Value string `json:"value"`
    Grain string `json:"grain"`
    Type string `json:"type"`
}

When I send a request with the text value 6 euro, I am getting:

{"body":"6 euro", "value": {"value":6, "type":"value", "unit":"EUR"}, "dim":"amount-of-money"}

As you can see, the inner value field now contains an integer instead of a string. When I parse the json string using the shown struct, the inner value value will be "".

How can I account for such an inconsistent json response?

0

There are 0 best solutions below