Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 527

Replace a field with the index of values in an array in JQ

$
0
0

Given the following JSON file:

{"quadrants": ["Languages + Frameworks","Tools","Platforms","Techniques"  ],"entries": [    {"quadrant": "Languages + Frameworks"    },    {"quadrant": "Platforms"    },    {"quadrant": "Languages + Frameworks"    }  ]}

How can the quadrant field of each entry be replaced with the index of the corresponding value in quadrants?

Expected output:

{"quadrants": ["Languages + Frameworks","Tools","Platforms","Techniques"  ],"entries": [    {"quadrant": 0    },    {"quadrant": 2    },    {"quadrant": 0    }  ]}

I tried the following jq script but get null for each quadrant.

jq '  .entries |= map(    .quadrant = (.quadrant as $q | (.quadrants | index($q)))  )'"$1"

Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>