-1

I want to add a new key into the pre-existing .json file.

  1. Is it possible to put the new key in the specific location? (ex) "SliceThickness" = 3 Add "SliceTiming" = 0.1 right after "SliceThickness" key
  2. How can I check the added one? I cannot see the added one in the pre-existing .json file.

    • I'm using python3 with the Jupyter notebook.
    • pre-existing .json file (name: task-rest_bold.json) looks like this

In short, I want to put "SliceTiming" right after "SliceThickness". Additionally, I want to check in on the .json file, because with the code below, it says that SliceTiming has been successfully added, however, I could not find it on the "task_rest_bold.json" file.

{
  "AcquisitionMatrixPE": 63,
  "AcquisitionNumber": 5,
  "AcquisitionTime": "08:06:20.100000",
  "BodyPartExamined": "BRAIN",
  "CogAtlasID": "TODO",
  "CoilString": "SENSE-Head-8",
  "ConversionSoftware": "dcm2niix",
  "ConversionSoftwareVersion": "v1.0.20190410  GCC6.3.0",
  "DeviceSerialNumber": "4sEr9bzYud6E",
  "EchoTime": 0.030001,
  "EchoTrainLength": 59,
  "FlipAngle": 80,
  "ImageOrientationPatientDICOM": [1, 0, 0, 0, 1, 0],
  "ImageType": [
    "ORIGINAL",
    "PRIMARY",
    "M",
    "FFE",
    "M",
    "FFE"
],
  "ImagingFrequency": 127.779,
  "InPlanePhaseEncodingDirectionDICOM": "COL",
  "InstitutionName": "OHSU_MRI3",
  "MRAcquisitionType": "2D",
  "MagneticFieldStrength": 3,
  "Manufacturer": "Philips",
  "ManufacturersModelName": "Intera",
  "Modality": "MR",
  "PartialFourier": 0.936508,
  "PatientPosition": "HFS",
  "PercentPhaseFOV": 93.75,
  "PhaseEncodingAxis": "j",
  "PhaseEncodingSteps": 63,
  "PhilipsRWVIntercept": 0,
  "PhilipsRWVSlope": 1.22833,
  "PhilipsRescaleIntercept": 0,
  "PhilipsRescaleSlope": 1.22833,
  "PhilipsScaleSlope": 0.000766453,
  "PixelBandwidth": 2110,
  "ProtocolName": "Resting_State_fMRI",
  "ReconMatrixPE": 64,
  "RepetitionTime": 3.001,
  "SAR": 0.0630149,
  "ScanOptions": "FS",
  "ScanningSequence": "GR",
  "SequenceVariant": "SK",
  "SeriesDescription": "Resting_State_fMRI",
  "SeriesNumber": 501,
  "SliceThickness": 3.313,
  "SoftwareVersions": "3.2.1_3.2.1.1",
  "SpacingBetweenSlices": 3.313,
  "TaskName": "TODO: full task name for rest",
  "UsePhilipsFloatNotDisplayScaling": 1,
  "dcmmeta_affine": [
    [-3.3125, 0.0, 0.0, 106.49729919433594],
    [0.0, 3.3125, 0.0, -96.91033935546875],
    [0.0, 0.0, 3.3125, -38.010536193847656],
    [0.0, 0.0, 0.0, 1.0]
],
  "dcmmeta_reorient_transform": [
    [0.0, -1.0, 0.0, 63.0],
    [1.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, -1.0, 47.0],
    [0.0, 0.0, 0.0, 1.0]
],
  "dcmmeta_shape": [64, 64, 48, 140],
  "dcmmeta_slice_dim": 2,
  "dcmmeta_version": 0.6,
  "global": {
    "const": {
      "AcquisitionDuration": 423.1409912109375,
      "AcquisitionMatrix": [64, 0, 0, 63],
      "AcquisitionNumber": 5,
      "BitsAllocated": 16,
      "BitsStored": 16,
      "BodyPartExamined": "BRAIN",
      "Columns": 64,
      "DiffusionBValue": 0.0
      }
   }
}

import json

# Loading .json file
with open('/home/rrt/MyProject/Nifti2/task-rest_bold.json', 'r') as f:
    json_data = json.load(f)

# Print .json file with indent
print(json.dumps(json_data,indent="\t"))

# Check if "SliceTiming" exists
if 'SliceTiming' not in json_data:
    TR = json_data["RepetitionTime"]
    nSlices = json_data["dcmmeta_shape"][3]
    TA = TR/nSlices
    # Calculate SliceTiming
    SliceTiming = [0:TA:TR]


# Put SliceTiming into the .json file
json_data["SliceTiming"]= SliceTiming

if "SliceTiming" in json_data:
    print(json_data["SliceTiming"])
Anwarvic
  • 12,156
  • 4
  • 49
  • 69

2 Answers2

1

Looking at other sources it doesn't seem possible to add a value in the middle of a json object.

The answer on this question: https://stackoverflow.com/a/23111697/6928839

opens the file with a write flag:

import json

with open(json_file) as json_file:
    json_decoded = json.load(json_file)

json_decoded['ADDED_KEY'] = 'ADDED_VALUE'

with open(json_file, 'w') as json_file:
    json.dump(json_decoded, json_file)

However, the value is appended to the json object.

  • I’m not used to handle .json file.... Is the order of the .json dictionary matters? Is it possible to find location or index of certain key? – user12077723 Sep 17 '19 at 07:37
  • @user12077723 once again: json objects are UNORDERED collections of key:value pairs. They have NO "index" nor "location". – bruno desthuilliers Sep 17 '19 at 11:33
  • @brunodesthuilliers is right. JSON files are not meant to be viewed raw so order doesn't matter. When a json file is used in code, the key is the only value needed to retrieve the data that is linked to that key. Don't worry too much about the order, just worry if the key and data pair is in the json file. – Hatergenerator Sep 17 '19 at 17:50
0

If you want the Json with keys in a specific order. Instead of add items {} afterwards. Consider construct the Json in the beginning, then update values. That way, the order can be pre-defined. For example:

json_obj = {}
json_obj["first"] = ""
json_obj["second"] = []
json_obj["third"] = {}
Robina Li
  • 1,158
  • 7
  • 4