0

In shell scripting, I can easily pass any output from a script/command to another script/command with pipe, as they work with lines. eg

cat file.txt | grep "name" | tr "[:lower:]" "[:upper:]"

How do I achieve the same feature with python, that will pass json output from one command to the other command? eg.

cat file.json | json_grep "name" | json_transform "lowercase" "uppercase"

where file.json will contain

{
  "name": "john",
  "age": "23"
}

the intermediate data that transfer between json_grep and json_transform will become

{
  "name": "john",
}

and the final output will become

{
  "name": "JOHN",
}

I'm not looking for function to implement the grep or the transform, as I already know how to use the JSON module in Python.

What I'm looking is how to pass the data.

I can use temporary text file, or database to store the intermediate data, but is there any better way than that?

Sharuzzaman Ahmat Raslan
  • 1,557
  • 2
  • 22
  • 34
  • Not the answer but, in bash this can be done in bit shorter way `awk -F: ' /name/{$2=toupper($2)}1' file.txt` Pipe is not needed. – P.... Mar 17 '17 at 07:04
  • you can load json file and parse it. – Karthikeyan KR Mar 17 '17 at 07:04
  • It's not really clear what you are asking, but does "read standard input, write to standard output" answer your question? This is a general Unix filter design question, not so much a Python (or Bash really, though certainly general shell) question. – tripleee Mar 17 '17 at 07:23
  • Starting a separate process for each processing step is very flexible but also suboptimal if the processing needs to happen very frequently. If you are writing the code in Python anyway, performing both transformations in the same script would certainly be more efficient, at the expense of hardcoding a particular behavior. – tripleee Mar 17 '17 at 07:24
  • @karthi writing the output to file, then read back for the next program seems not really efficent – Sharuzzaman Ahmat Raslan Mar 17 '17 at 07:33
  • @triplee I want to split the task similar to microservice, but for shell command. shell command pipes lines, but this is using json. – Sharuzzaman Ahmat Raslan Mar 17 '17 at 07:38
  • Are you figuring it's a problem that JSON is split over multiple lines? That's a problem in your Python code, not in the pipeline. – tripleee Mar 17 '17 at 07:43

0 Answers0