Skip to content

Conversation

jadutter
Copy link

@jadutter jadutter commented May 14, 2024

  • toarray/0 if the input is not an array, put it into an array
  • toobject/0 if the input is not an object, put it in an object mapped to the key named "value"
  • getuuid/0 return a string that is a UUID
  • add_uuids/0 ensure the input is an array of objects, and set each item to use a UUID
  • add_ids/0 ensure the input is an array of objects, and set each item to use a unique id (the index number)
  • add_uuids/1 similar to add_uuids/0, but use a custom key name
  • add_ids/1 similar to add_ids/0, but use a custom key name
# [
#     {
#         "fruit": "apple"
#     }
# ]
jq -n '
    {"fruit":"apple"} |
    toarray
'
# [
#     {
#         "value": "apple"
#     },
#     {
#         "value": "banana"
#     },
#     {
#         "value": "orange"
#     }
# ]
jq -n '
    ["apple", "banana", "orange"] |
    map(toobject)
'
# "118099DD-0314-6FC1-710E-F1B63036D404"
jq -n 'getuuid'
# [
#     {
#         "value": "a",
#         "key": "B461CC13-5A37-2FA7-A352-EC4419EA158B"
#     },
#     {
#         "value": "b",
#         "key": "070B47A5-F32A-5C3C-2FF2-627997B87A17"
#     },
#     {
#         "value": "c",
#         "key": "785F1BB7-FAAE-9F21-1BEE-C2BE126DE286"
#     }
# ]
jq -n '
    ["a","b","c"] |
    add_ids("key")
'
# [
#     {
#         "value": "a",
#         "id": "9C0E3357-196D-0D58-FCCE-989C0005C160"
#     },
#     {
#         "value": "b",
#         "id": "AD591BA4-7D6E-8D26-28DF-57BC5E53B874"
#     },
#     {
#         "value": "c",
#         "id": "387BCED5-CD53-5535-2D7C-74AE2636A0B9"
#     }
# ]
jq -n '
    ["a","b","c"] |
    add_uuids("id")
'

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.

1 participant