1

I have a .json file that I need to extract the SliceTiming field from into a separate txt file. I cannot use jq. If I could, my command would be

jq .SliceTiming[] *.json

Here is the .json file

{
"EchoTime": 0.033,
"RepetitionTime": 0.75,
"EffectiveEchoSpacing": 0.0005,
"SliceTiming": [
    0.51,
    0,
    0.365,
    0.0725,
    0.4375,
    0.2175,
    0.5825,
    0.2925,
    0.6575,
    0.145,
    0.51,
    0,
    0.365,
    0.0725,
    0.4375,
    0.2175,
    0.5825,
    0.2925,
    0.6575,
    0.145,
    0.51,
    0,
    0.365,
    0.0725,
    0.4375,
    0.2175,
    0.5825,
    0.2925,
    0.6575,
    0.145,
    0.51,
    0,
    0.365,
    0.0725,
    0.4375,
    0.2175,
    0.5825,
    0.2925,
    0.6575,
    0.145,
    0.51,
    0,
    0.365,
    0.0725,
    0.4375,
    0.2175,
    0.5825,
    0.2925,
    0.6575,
    0.145,
    0.51,
    0,
    0.365,
    0.0725,
    0.4375,
    0.2175,
    0.5825,
    0.2925,
    0.6575,
    0.145   ],
"PhaseEncodingDirection": "j-"}
  • If you can't use proper tools like jq, any guarantees how the json file is formatted wrt. white space, line endings etc.? – dirkt Nov 26 '18 at 17:53
  • Are you allowed to use python? – Mark Plotnick Nov 26 '18 at 17:56
  • python is allowed although I would prefer to use grep, sed, or awk if possible. – Noah Nov 26 '18 at 17:57
  • 1
    Why can't you use jq? – Kusalananda Nov 26 '18 at 18:16
  • I am running on a server, which does not have jq installed and I can't install jq or anything else. – Noah Nov 26 '18 at 18:20
2

Seeing as python is allowed. This would be my preferred solution.

I don't feel comfortable dealing with sed, grep, awk as I do not know edge cases about the data.

Print array with , to stdout

cat data.json | python -c "import sys, json; print json.load(sys.stdin)['SliceTiming']"

Note: This example uses python2.7, unlikely to work with python3.x

Just the values output. No [] no ,:

Created slice_out.py as I couldn't find a nice way to do inline terminal

import sys, json
slice_data = json.load(sys.stdin)['SliceTiming']
for s in slice_data: print s

Ran with cat data.json | python slice_out.py

  • Great thank you! The output is another array in the format [ XX , XX , XX ,...,XX ]. I would like to get the output as each number on a separate line without commas. Is there a way to do this? – Noah Nov 26 '18 at 18:05
  • I've added another solution. Let me know if that works for you – Dean coakley Nov 26 '18 at 18:59
1

Using Perl and the JSON module:

perl -MJSON -e 'print join("\n", @{decode_json(join("\n",<>))->{"SliceTiming"}})' <file.json

This outputs each number on a separate line without commas

  • Thank you! I guess I can't use this because i get an error. Can't locate JSON.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .). BEGIN failed--compilation aborted. – Noah Nov 26 '18 at 18:10
  • @Noah I guess you just have to install the JSON Perl module. It should be available as a package on your Unix (just like jq). – Kusalananda Nov 26 '18 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.