Lädt...


🔧 How to Use JQ to find Key based on Value


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

USECASE

The aim of this page is to explain how to use jq to find a key based on a value in JSON data retrieved from Consul.

Consul KV Store: Used to store key-value pairs.

JSON Data: Example JSON data retrieved from Consul.

{
  "a3e705d1-98d3-4c58-80a4-f9f9c9ec3e47": "CUSTOMER_1",
  "d4b7e8e9-1b50-4f3d-b3d9-ea6b5feca1bd": "CUSTOMER_2",
  "b21c9e36-4798-4387-ae1e-f6c678b0c14e": "CUSTOMER_3"
}

Objective: Find the key corresponding to a specific value.

jq Command: Tool for processing JSON data.

Basic jq Command:

jq -r 'to_entries[] | select(.value == "VALUE_YOU_ARE_LOOKING_FOR") | .key' yourfile.json

Integrating with Consul:

consul kv get global/uuid-to-customer-name | jq -r 'to_entries[] | select(.value == "VALUE_YOU_ARE_LOOKING_FOR") | .key'

Example Value: Searching for the value "anonymized_customer".

Step-by-Step Breakdown:

  • Retrieve JSON from Consul:
consul kv get global/uuid-to-customer-name
  • Convert JSON to Key-Value Pairs:
consul kv get customer-uuids | jq -r 'to_entries[]'
  • Output:
{
  "key": "a3e705d1-98d3-4c58-80a4-f9f9c9ec3e47",
  "value": "CUSTOMER_1"
}
{
  "key": "d4b7e8e9-1b50-4f3d-b3d9-ea6b5feca1bd",
  "value": "CUSTOMER_2"
}
{
  "key": "b21c9e36-4798-4387-ae1e-f6c678b0c14e",
  "value": "CUSTOMER_3"
}

  • Filter for Specific Value:
consul kv get global/uuid-to-customer-name | jq -r 'to_entries[] | select(.value == "anonymized_customer")'
  • Output:
{
  "key": "cde71158-f1d6-4707-88e0-28d1716f2790",
  "value": "anonymized_customer"
}
  • Extract the Key:
consul kv get global/uuid-to-customer-name | jq -r 'to_entries[] | select(.value == "anonymized_customer") | .key'
  • Output:
cde71158-f1d6-4707-88e0-28d1716f2790

LINKS

...

🔧 How to Use JQ to find Key based on Value


📈 30.53 Punkte
🔧 Programmierung

🔧 Find the maximum value of the K-th smallest usage value in Array


📈 25.05 Punkte
🔧 Programmierung

🐧 Find an Element in DOM Based on an Attribute Value


📈 20.49 Punkte
🐧 Linux Tipps

🐧 Sort a Map in Go by Value (Sort Map by Value)


📈 18.09 Punkte
🐧 Linux Tipps

🐧 How to Define a Default Value for “input type=text” Without Using Attribute ‘value’?


📈 18.09 Punkte
🐧 Linux Tipps

🕵️ Medium CVE-2020-28281: Set-object-value project Set-object-value


📈 18.09 Punkte
🕵️ Sicherheitslücken

🔧 Why can't I store a value and a reference to that value in the same struct?


📈 18.09 Punkte
🔧 Programmierung

🔧 Exploring the Core of true value vs false value in JavaScript...


📈 18.09 Punkte
🔧 Programmierung

🔧 #63 — Search for The Nth Value And The Nth Value from Last


📈 18.09 Punkte
🔧 Programmierung

🔧 SQL, in each group modify the null value of a specified column as its neighboring value #eg43


📈 18.09 Punkte
🔧 Programmierung

🔧 #24 — Repeat the Value of Each Cell N times According to the Value of the Neighboring cell


📈 18.09 Punkte
🔧 Programmierung

🕵️ CVE-2020-11812 | Rukovoditel 2.5.2 filters[0][value]/filters[1][value] sql injection


📈 18.09 Punkte
🕵️ Sicherheitslücken

🐧 One key to rule them all: Recovering the master key from RAM to break Android's file-based encryption


📈 16.71 Punkte
🐧 Linux Tipps

🐧 How Does ssh-copy-id Determine Which SSH Key to Use by Default When No Key Is Specified?


📈 16.15 Punkte
🐧 Linux Tipps

🔧 How to use your Thunderbird-generated OpenPGP key to set your Web Key Directory(WKD)


📈 16.15 Punkte
🔧 Programmierung

🐧 What is your MOD4 (Windows key) key cap that you use on your Linux machines?


📈 16.15 Punkte
🐧 Linux Tipps

🪟 How To Find The Seed Value For A Minecraft World On Windows 10


📈 16 Punkte
🪟 Windows Tipps

📰 Time for businesses to move past generative AI hype and find real value


📈 16 Punkte
📰 IT Security Nachrichten

📰 How To Find The Seed Value For A Minecraft World On Windows 10


📈 16 Punkte
🖥️ Betriebssysteme

🔧 In Excel, Find the Maximum Value and the Neighboring N Members Before and After


📈 16 Punkte
🔧 Programmierung

🐧 Find the Minimum Value in a Java Array


📈 16 Punkte
🐧 Linux Tipps

🔧 Find two nodes in the BST whose product equals to the target value


📈 16 Punkte
🔧 Programmierung

🔧 Find that the value of 2^{2n} is equal to O(2^n) or not


📈 16 Punkte
🔧 Programmierung

🔧 Find and remove maximum value in each row of a given Matrix


📈 16 Punkte
🔧 Programmierung

📰 Hacker develops new 'Screenshotter' malware to find high-value targets


📈 16 Punkte
📰 IT Security Nachrichten

📰 Hacker develops new 'Screenshotter' malware to find high-value targets


📈 16 Punkte
📰 IT Security Nachrichten

🔧 Find most frequent value of ancestors for each Node of given Tree


📈 16 Punkte
🔧 Programmierung

matomo