MCPcopy
hub / github.com/keploy/keploy / normalizeNestedJSONValue

Function normalizeNestedJSONValue

pkg/matcher/utils.go:1785–1811  ·  view source on GitHub ↗

normalizeNestedJSONValue walks the parsed JSON value and replaces any string fields whose key is in rootKeys and whose value is itself valid JSON with the parsed inner JSON.

(v interface{}, rootKeys map[string]bool)

Source from the content-addressed store, hash-verified

1783// string fields whose key is in rootKeys and whose value is itself valid
1784// JSON with the parsed inner JSON.
1785func normalizeNestedJSONValue(v interface{}, rootKeys map[string]bool) {
1786 switch t := v.(type) {
1787 case map[string]interface{}:
1788 for k, val := range t {
1789 lowerKey := strings.ToLower(k)
1790
1791 // If this key is in rootKeys and the value is a JSON string, unwrap it.
1792 if rootKeys[lowerKey] {
1793 if s, ok := val.(string); ok && pkg.LooksLikeJSON(s) && json.Valid([]byte(s)) {
1794 var inner interface{}
1795 if err := json.Unmarshal([]byte(s), &inner); err == nil {
1796 t[k] = inner
1797 val = inner
1798 }
1799 }
1800 }
1801
1802 // Recurse into children to handle deeper nested wrappers.
1803 normalizeNestedJSONValue(val, rootKeys)
1804 }
1805
1806 case []interface{}:
1807 for i := range t {
1808 normalizeNestedJSONValue(t[i], rootKeys)
1809 }
1810 }
1811}

Callers 1

Calls 3

LooksLikeJSONFunction · 0.92
ValidMethod · 0.80
UnmarshalMethod · 0.80

Tested by

no test coverage detected