MCPcopy
hub / github.com/tidwall/gjson / modFlatten

Function modFlatten

gjson.go:3055–3095  ·  view source on GitHub ↗

@flatten an array with child arrays. [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,[6,7]] The {"deep":true} arg can be provide for deep flattening. [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,6,7] The original json is returned when the json is not an array.

(json, arg string)

Source from the content-addressed store, hash-verified

3053//
3054// The original json is returned when the json is not an array.
3055func modFlatten(json, arg string) string {
3056 res := Parse(json)
3057 if !res.IsArray() {
3058 return json
3059 }
3060 var deep bool
3061 if arg != "" {
3062 Parse(arg).ForEach(func(key, value Result) bool {
3063 if key.String() == "deep" {
3064 deep = value.Bool()
3065 }
3066 return true
3067 })
3068 }
3069 var out []byte
3070 out = append(out, '[')
3071 var idx int
3072 res.ForEach(func(_, value Result) bool {
3073 var raw string
3074 if value.IsArray() {
3075 if deep {
3076 raw = unwrap(modFlatten(value.Raw, arg))
3077 } else {
3078 raw = unwrap(value.Raw)
3079 }
3080 } else {
3081 raw = value.Raw
3082 }
3083 raw = strings.TrimSpace(raw)
3084 if len(raw) > 0 {
3085 if idx > 0 {
3086 out = append(out, ',')
3087 }
3088 out = append(out, raw...)
3089 idx++
3090 }
3091 return true
3092 })
3093 out = append(out, ']')
3094 return bytesString(out)
3095}
3096
3097// @keys extracts the keys from an object.
3098//

Callers

nothing calls this directly

Calls 7

ParseFunction · 0.85
unwrapFunction · 0.85
bytesStringFunction · 0.85
IsArrayMethod · 0.80
ForEachMethod · 0.80
BoolMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…