gjson icon indicating copy to clipboard operation
gjson copied to clipboard

Is there a way of listing all available paths?

Open moul opened this issue 4 years ago • 1 comments

Basically my need is to filter-out some values if the keys match a specific rule

I can use sjson to set the values but I need a way of listing all the map keys available

From what I can see on godoc, there is no helper for this yet but I was wondering if someone that knows how gjson works under the hood if and how it can be implemented

Thank you 🙏

moul avatar Feb 24 '20 08:02 moul

I had a similar need just now to list all map keys (your 2nd paragraph). Here's a gist of what I came up with.

It's comprised of 3 files, one of them being a go test file. The type JsonKey is a string but composed of the full path. Therefore both fld_string_value and deeply.nested.object.fld_string_value are valid JsonKeys.

In "walk-json_test.go", uncomment line 88 to print all paths to the console.

Note how inexact it is to match raw json to a type (eg float vs int vs nil). Really some sort of schema or data-definition is required to enforce types. Towards that end, I think walk-json.go could be paired with other objects to manage schema type conversation, do validations and even run event actions.

jpfluger avatar Jun 01 '20 03:06 jpfluger