Skip to content

Conversation

@n3wtype
Copy link

@n3wtype n3wtype commented Apr 2, 2025

Pull request adds key2obj modifier function. It was made to allow handling of the APIs responses like below, which instead of providing a list, nest objects under parent object keys effectively representing child object name/identifier. key2obj appends key from parent to children under "_key". This creates flat structure which can be processed further without loosing information from parent key.

It could be usefull for apps which heavily depend on user configurable JSON queries, handling generic data, from wild APIs (occured to me on telegraf with json_v2 parsers and ).

		{
			"tickets": {
				"ISSUE-10": {
						"subject": "foo",
						"priority": 1,
						"id": 123
				},
				"ISSUE-12": {
						"subject": "bar",
						"priority": 3,
						"id": 124
				},
				"ISSUE-17": {
						"subject": "baz",
						"priority": 2,
						"id": 1128
				}
			}
		}
tickets.@key2obj

		{
			"tickets": {
				"ISSUE-10": {
						"subject": "foo",
						"priority": 1,
						"id": 123,
						"_key": "ISSUE-10"
				},
				"ISSUE-12": {
						"subject": "bar",
						"priority": 3,
						"id": 124,
						"_key": "ISSUE-10"
				},
				"ISSUE-17": {
						"subject": "baz",
						"priority": 2,
						"id": 1128,
						"_key": "ISSUE-10"
				}
			}
		}
tickets.@key2obj.@values
			[{
					"subject": "foo",
					"priority": 1,
					"id": 123,
					"_key": "ISSUE-10"
			},
			{
					"subject": "bar",
					"priority": 3,
					"id": 124,
					"_key": "ISSUE-10"
			},
			{
					"subject": "baz",
					"priority": 2,
					"id": 1128,
					"_key": "ISSUE-10"
			}]

i++
return true
})
out.WriteByte(']')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writes { at the start but closes with ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants