Home > Back-end >  Filtering Live json Stream on linux
Filtering Live json Stream on linux

Time:03-26

I have live raw json stream data from the virtual radar server I'm using.

I use Netcat to fetch the data and jq to save it on my kali linux. using the following command.

nc 127.0.0.1 30006 | jq > A7.json

But i want to filter specific content from the data stream.

i use the following command to extract the data.

cat A7.json | jq '.acList[] | select(.Call | contains("QTR"))' - To fetch Selected airline

But i realized later that the above command only works once. in other words, it does not refresh. as the data is updating every second. i have to execute the command over and over again to extract the filter data which is causing to generate duplicate data.

Can someone help me how to filter the live data without executing the command over and over .

CodePudding user response:

As you don't use the --stream option, I suppose your document is a regular JSON document.

To execute your command every second, you can implement a loop that sleeps for 1 second:

while true; do sleep 1; nc 127.0.0.1 30006 | jq '.acList[] | select(…)'; done

To have the output on the screen and also save to a file (like you did with A7.json), you can add a call to tee:

# saves the document as returned by `nc` but outputs result of `jq`
while true; do sleep 1; nc 127.0.0.1 30006 | tee A7.json | jq '.acList[] | …'; done

# saves the result of `jq` and outputs it
while true; do sleep 1; nc 127.0.0.1 30006 | jq '.acList[] | …' | tee A7.json; done

CodePudding user response:

You need to tell nc to keep listening with -k :

nc -k -l 30006 | jq '.acList[] | select(.Call | contains("QTR"))'
  • Related