How does shell interpret a JSON string?
You can use the jq command to parse JSON strings in a shell.
jq is a highly powerful command-line tool used for manipulating JSON data. It supports various operations such as selecting, filtering, transforming, modifying, and formatting JSON data.
Here are some examples of parsing JSON strings using jq:
- Install the jq command (if not already installed):
- # On Ubuntu, run the command “sudo apt-get install jq”
# On CentOS, run the command “sudo yum install jq” - Parse JSON string.
- Use jq to parse the JSON string located in a file named data.json containing the following data:
{
“name”: “John”,
“age”: 30,
“city”: “New York”
} - Output results:
- {
“name”: “John”,
“age”: 30,
“location”: “New York”
} - Choose specific attributes:
- Selecting specific attribute using jq:
jq ‘.name’ data.json - Output:
- The name of the person is John.
- Filtering data:
- Use jq to filter data where the age is greater than 25 in data.json file.
- The output result:
- {
“name”: “John”,
“age”: 30,
“city”: “New York”
}
can be paraphrased as:
{
“name”: “John”,
“age”: 30,
“residence”: “New York”
}
These are just some examples of jq commands, it has many other features and options available. You can refer to jq’s official documentation for more detailed information.