Search notes:

jq: to_entries

Passing JSON Objects to to_entries

If a JSON object is passed to to_entries, it creates an array of JSON object each of which has the two keys key and value.
$ echo '{
  "txt":  "Hello world",
  "num":   42
}'                           | jq to_entries
[
  {
    "key": "txt",
    "value": "Hello world"
  },
  {
    "key": "num",
    "value": 42
  }
]
If the input is an array of objects, it should probably be passed to the .[] filter:
$ echo '
[
  {
    "txt":  "Hello world",
    "num":   42
  },
  {
    "key-one":  "val 1",
    "key-two":  "val 2"
  }
]'                           | jq ' .[] | to_entries'
[
  {
    "key": "txt",
    "value": "Hello world"
  },
  {
    "key": "num",
    "value": 42
  }
]
[
  {
    "key": "key-one",
    "value": "val 1"
  },
  {
    "key": "key-two",
    "value": "val 2"
  }
]

Passing an array to to_entries

If an array is passed to to_entries, the values for the key correspond to the indices in the array:
$ echo '[ "foo", "bar", "baz" ]' | jq to_entries
[
  {
    "key": 0,
    "value": "foo"
  },
  {
    "key": 1,
    "value": "bar"
  },
  {
    "key": 2,
    "value": "baz"
  }
]

See also

compare with to_entries[]

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1738302576, '13.59.116.142', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/JavaScript/JSON/tools/jq/functions/to_entries(109): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78