ohler55 / ojg

Optimized JSON for Go
MIT License
837 stars 50 forks source link

Cannot delete from array? #174

Closed Skeeve closed 3 months ago

Skeeve commented 3 months ago
package main

import (
    "fmt"

    "github.com/ohler55/ojg"
    "github.com/ohler55/ojg/jp"
    "github.com/ohler55/ojg/sen"
)

func main() {
    data := map[string]any{"a": 1, "b": []any{0, 1, "2", 3, 4, 5}, "c": 3}
    if err := jp.MustParseString("$.b[2]").Del(data); err != nil {
        panic(err)
    }
    fmt.Println(sen.String(data, &ojg.Options{Sort: true}))

}

I would have expected the output to be:

{a:1 b:[0 1 3 4 5] c:3}

But instead it is

{a:1 b:[0 1 null 3 4 5] c:3}

Is this a misunderstanding on my part or a bug?

ohler55 commented 3 months ago

That is expected behavior. If you want to not just remove a value but also collapse the array to fill in the removed item the jp.MustModify() is the function you are looking for.

Skeeve commented 3 months ago

And what about jp.Remove()?

ohler55 commented 3 months ago

Ah yes, jp.Remove() also works. Probably easier in this case too.

Skeeve commented 3 months ago

Are there any examples for MustModify? I find the documentation a bit confusing for someone who just discovered JSONPath.

Also there are some typos. pinic instead of panic for example.

ohler55 commented 3 months ago

There are unit tests for all the functions in the same directory as the code (ojg/jp). That probably the best place to start.

There are some links to the JSONPath spec in the README.md although that can get pretty overwhelming.

ohler55 commented 3 months ago

The docs for jp.MustModify were updated in develop if you would like to check it out.

ohler55 commented 3 months ago

Okay to close this?

Skeeve commented 3 months ago

I wasn't able to check the docs, but fine with me.