LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2016, 10:12 AM   #1
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Rep: Reputation: 73
Question JSON output: check for true or false based on name using bash


Hello, I have a json data in which I have different names, lets call them servers, which can be in 2 states: true or false.

How is the best way to check a certain name/server if it's true or false state?

JSON example:
Code:
[{"name":"TEST1","state":true},{"name":"TEST2","state":false},{"name":"TEST3","state":true},{"name":"TEST4","state":false}]
The checks should be done only for one of the names, basically name defines a machine and state defines if it's online or offline.

If I haven't explain the best, please exclude the confusion, please ask any question.
 
Old 10-21-2016, 10:18 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,200

Rep: Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307
Are you able to install a command-line JSON parser? Several recommendations in this thread:

Unix command-line JSON parser?

If not, do you have a scripting language interpreter (Perl or Python) with JSON parsing libraries, that you can pipe stuff into?
 
Old 10-21-2016, 10:20 AM   #3
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Quote:
Originally Posted by dugan View Post
Are you able to install a command-line JSON parser? Several recommendations in this thread:

Unix command-line JSON parser?

If not, do you have a scripting language interpreter (Perl or Python) with JSON parsing libraries, that you can pipe stuff into?
Not really, I should stick to bash/sh. There are some reason which I can't control.
 
Old 10-21-2016, 12:51 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,000

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
bash is not really the correct option here (tool/job). Generally all distros these days have both Perl and Python (my preference is Ruby but it does not come by default ), so either should be easily available and ready to solve your problem. A quick search yields many results on how to use either as the solution
 
Old 10-21-2016, 01:17 PM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Try something like this:
Code:
$ sed -rn 's/.*TEST2" *, *"state":([a-z]+).*/\1/p' /tmp/in.json
false
or with variable
Code:
$ server=TEST2
$ sed -rn 's/.*'$server'" *, *"state":([a-z]+).*/\1/p' /tmp/in.json
false
 
1 members found this post helpful.
Old 10-21-2016, 02:07 PM   #6
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Quote:
Originally Posted by firstfire View Post
Hi.

Try something like this:
Code:
$ sed -rn 's/.*TEST2" *, *"state":([a-z]+).*/\1/p' /tmp/in.json
false
or with variable
Code:
$ server=TEST2
$ sed -rn 's/.*'$server'" *, *"state":([a-z]+).*/\1/p' /tmp/in.json
false
This is perfect, thank you!
 
Old 10-21-2016, 03:02 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,000

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Glad you got your solution. Hopefully your data is never over multiple lines or more deeply nested

Here is a quick idea of what the other option might be:
Code:
ruby -r json -ne 'x=JSON.parse($_, symbolize_names: true);x.each{|n| puts n[:state] if n[:name] == "TEST2"}' file
It is of course longer but as mentioned above, if the data should become more complex, the sed or other type solution will struggle greatly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script - check if three commands are true edsmith123 Linux - Newbie 3 01-31-2015 11:49 AM
Write an OS in C++??? True or false? maples Linux - Newbie 25 12-29-2013 02:21 PM
[SOLVED] diff - how do i read it as true or false? Markus72 Linux - Software 3 03-27-2011 04:13 AM
comparison is always true/false jubaitca Programming 20 11-05-2006 06:55 PM
true or false? alaios Programming 7 07-16-2005 10:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration