How to "number" array items?

Martin Mucha

Considering input:

[
  {
    "a": 1
  },
  {
    "a": 2
  },
  {
    "a": 7
  }
]

how do I add new field to each object, which value would be index in array? Producing:

[
  {
    "a": 1,
    "index": 0
  },
  {
    "a": 2,
    "index": 1
  },
  {
    "a": 7,
    "index": 2
  }
]
choroba

Store the structure into a variable, then use keys to get the indices, retrieve the corresponding object from the variable using the index and add the index to it:

jq '[ . as $d | keys[] | $d[.] + {index:.} ]' file.json

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to view items in array

分類Dev

Count number of items in an array with a specific property value

分類Dev

How to prevent duplicate items in array?

分類Dev

How to separate items from array?

分類Dev

How to Deserialize XML Array items?

分類Dev

How to List items in array javascript

分類Dev

How to split file items into array?

分類Dev

How to reduce the number of items in the TFS workspace

分類Dev

How to convert array of items to array of objects?

分類Dev

Swift - How to get indexes of filtered items of array

分類Dev

Swift - How to get indexes of filtered items of array

分類Dev

How to delete items from array in .txt C

分類Dev

RxJs how to map the items in an Observable<Array>

分類Dev

How to count items in arrays of values in an array of hashes

分類Dev

How to access individual items in serialized array?

分類Dev

How to add array items in one single item?

分類Dev

How to store a number array in MongoDB

分類Dev

How to count price of each item looking to number of items?

分類Dev

How can I change the number of items that end up in a list

分類Dev

Returning the number of items in a list

分類Dev

Live Update the Number of Items

分類Dev

Specify number of items in tuple

分類Dev

How BigInteger convert a byte array to number in Java?

分類Dev

How to update an array with his index number in Mongoose

分類Dev

How can I search for the number in the array?

分類Dev

how to split array into given number in javascript

分類Dev

How to reduce the following array down to a single number?

分類Dev

How to find the most frequently repeated number in Array?

分類Dev

How to get the number of elements in a JSON array?

Related 関連記事

ホットタグ

アーカイブ