mlevit
mlevit

Reputation: 2736

Create single dictionary with jq

I have the following input:

[
  {
    constant_id: 5,
    object_id: 2,
    object_type: 'delimited_file',
    name: 'data_file_pattern',
    value: 'list_of_orders.csv',
    insert_date: 2021-11-23T10:24:16.568Z,
    update_date: null
  },
  {
    constant_id: 6,
    object_id: 2,
    object_type: 'delimited_file',
    name: 'header_count',
    value: '1',
    insert_date: 2021-11-23T10:24:16.568Z,
    update_date: null
  }
]

That I'd like to combine to get the following result:

{
  data_file_pattern: 'list_of_orders.csv',
  header_count: '1'
}

Basically creating a single dictionary with only the name and value keys from the input dictionaries. I believe I've done this before but for the life of me I can't figure it out again.

Upvotes: 0

Views: 534

Answers (1)

pmf
pmf

Reputation: 36033

If you get your quoting right in the input JSON, it's as simple as calling the from_entries builtin. It converts an array of objects to a single object with given key/value pairs. It takes the field name from a field called key, Key, name or Name and the value from a field called value or Value (see Demo):

from_entries
{
  "data_file_pattern": "list_of_orders.csv",
  "header_count": "1"
}

Note: I believe the second field name should read header_count instead of delimited_file as you wanted to take its name from .name, not .object_type.

Upvotes: 1

Related Questions