Turning a JSON object into a BASH array [duplicate] - json

This question already has answers here:
Bash Store Curl Results into Array
(1 answer)
Accessing a JSON object in Bash - associative array / list / another model
(7 answers)
How to get key names from JSON using jq
(9 answers)
Closed 4 years ago.
I have a URL
http://localhost/status?json
Which is PHP-FPM's status page, it outputs this
{
"pool":"www",
"process manager":"dynamic",
"start time":1526919087,
"start since":69780,
"accepted conn":403320,
"listen queue":0,
"max listen queue":0,
"listen queue len":0,
"idle processes":21,
"active processes":6,
"total processes":27,
"max active processes":200,
"max children reached":1,
"slow requests":0
}
I want to turn this JSON into an Array in Bash, so I can do a loop around it to check for stuff, I've heard JQ can parse this, but I'm unsure in BASH how I can convert it to a useable array.
Any ideas?

Related

Converting query on large JSON file to use a stream - jq [duplicate]

This question already has answers here:
Process large JSON stream with jq
(3 answers)
Improving performance when using jq to process large files
(3 answers)
Closed 4 months ago.
I have a large JSON file (2.7gb) that I would like filter for only the data I'm interested in to make the file smaller. The data consists of an array of objects. The following query works on a small subset of the JSON file, but when I try to run it on the 2.7gb file it doesn't work. How can I convert this into a stream query so that it can process the entire file?
.[] | {
food: .foodClass,
description: .description,
foodNutrients: [.foodNutrients[] | { nutrients: .nutrient, amount: .amount}],
upc: .gtinUpc,
servingSize: .servingSize,
servingSizeUnit: .servingSizeUnit,
ingredients: .ingredients,
fdcId: .fdcId,
dataType: .dataType,
brandOwner: .brandOwner,
marketCountry: .marketCountry,
brandedFootCategory: .brandedFoodCategory
}

Shell Script jq reading a json key with period (.) inside a variable [duplicate]

This question already has answers here:
Passing bash variable to jq
(10 answers)
"Invalid numeric literal" error from jq trying to modify JSON with variable
(1 answer)
Closed 4 years ago.
Here is the example json
{
"app": "K8s",
"version": "1.8",
"date": "2018-10-10"
}
In order to get the value of app, I can do this in jq as
jq '.app'
But what I want is, I want to pass the key to jq as a bash variable, i.e
bash_var="app"
jq '."${bash_var}"'
I'm getting the output as null instead of the value. What is the correct syntax to achieve this?
First, you need to port the bash variable into jq's context usign the --arg flag and access it inside the [..]
jq --arg keyvar "$bash_var" '.[$keyvar]' json

How to read a basic json array into a bash array [duplicate]

This question already has an answer here:
JSON list (not object) to Bash array?
(1 answer)
Closed 2 years ago.
I have an json formatted array as follows:
myArray='["GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN"]'
How do I read this simple json array into a bash array? I have seen plenty of specific answers for specific problems on this but haven't been able to find an answer for the basic version of it.
What I want is an array a=(GREEN GREEN GREEN ...)
I have tried readarray -t array <<< $myArray. This strips the quotes but doesn't create an actual array.
It looks like you want a regular array, not an associative one. Just use jq to turn the JSON into a form suitable for use with the mapfile/readarray builtin:
#!/usr/bin/env bash
myArray='["GREEN","GREEN","GREEN","GREEN","GREEN"]'
readarray -t a <<< "$(jq -r '.[]' <<< "$myArray")"
echo "a has ${#a[#]} elements. First one is ${a[0]}."
will output
a has 5 elements. First one is GREEN.
Note that this won't work right if any of the array elements have newlines in them.

How to parse json file, where keys start with '--' characters (dash) [duplicate]

This question already has answers here:
jq not working on tag name with dashes and numbers
(2 answers)
Escape field name in jq that contains '#' and '-'? [duplicate]
(2 answers)
Closed 3 years ago.
I have below JSON file and would like to update the values of these keys --resType and --LogLevel using jq.
{
"--resType": "FILE",
"--LogLevel": "INFO"
}
To do that, I am using below command.
./jq .--resType=Test config.json>test.json
But, I see an error that says
"jq: error: Test/0 is not defined at , line 1:".
"jq: 2 compile errors"

bash grep part of a json array [duplicate]

This question already has answers here:
Parsing JSON with Unix tools
(45 answers)
Closed 5 years ago.
Inside my bash script I've created a function that returns the following JSON array:
{
"access_token": "sjdhjdYdjsdbhggjhSJ78dbdsdbMhd=",
"scopes": "repository:delete repository:admin repository:write project:write team:write account:write",
"expires_in": 3600,
"refresh_token": "skdjKJDJDBNBDs",
"token_type": "bearer"
}
Now I wan't to grep the access token between the "". So I got sjdhjdYdjsdbhggjhSJ78dbdsdbMhd=
I've created a regex like below but this returns a grep: invalid repetition count(s) error.
grep -P '\{\s*"access_token"\s*:\s*(.+?)\s*\"' foobar.txt
Since you're dependending on nonstandard, nonportable functionality (in the form of grep -P), you might as well switch to a tool that's built for the job at hand (and actually understands JSON syntax, so it'll work even if your input has, say, a newline between the key and value, so long as it's syntactically valid):
jq -r .access_token <foobar.txt