简体   繁体   中英

Unify JSON files with jq

I'm new in the community and I'm not a dev, but I have a task I need to find a solution for. I hope I can get your ideas.

I have a set of JSON files. I want to be able to use jq or a command line that can help me unify the files into one single file.

For example:

File 1, has the following format:

{
  "Interactions": [
    {
      "ID": "ispring://presentations/F7385CB7-DFDC-4D05-90FA-B927DB3D170D/quizzes/",
      "Type": "2",
      "TimestampUtc": "8/27/2020 12:09:54 PM",
      "Timestamp": "8/27/2020 12:09:54 PM",
      "Weighting": "",
      "Result": "1",
      "Latency": "1000",
      "Description": "What is the purpose of Summarizing next steps?\n\nSelect the correct box or boxes",
      "LearnerResponse": "0_correct_answer[,]1_Rectangle_2",
      "ScormActivityId": "12392705",
      "InteractionIndex": "3",
      "AULMRID": "38093846"
    },
  ]
}

File 2:

{
  "Interactions": [
    {
      "ID": "ispring://presentations/CAA34147-7B48-40C6-84FD-5CE8077DB2BF/quizzes/",
      "Type": "2",
      "TimestampUtc": "12/8/2020 6:19:12 PM",
      "Timestamp": "12/8/2020 6:19:12 PM",
      "Weighting": "",
      "Result": "1",
      "Latency": "1300",
      "Description": "'Can't do' language tends to relay this impression...\n\nSelect one.",
      "LearnerResponse": "4_All_of_the_above",
      "ScormActivityId": "13334358",
      "InteractionIndex": "3",
      "AULMRID": "40715598"
    },
  ]
}

And this is my expected result in a third file:

{
  "Interactions": [
    {
      "ID": "ispring://presentations/F7385CB7-DFDC-4D05-90FA-B927DB3D170D/quizzes/",
      "Type": "2",
      "TimestampUtc": "8/27/2020 12:09:54 PM",
      "Timestamp": "8/27/2020 12:09:54 PM",
      "Weighting": "",
      "Result": "1",
      "Latency": "1000",
      "Description": "What is the purpose of Summarizing next steps?\n\nSelect the correct box or boxes",
      "LearnerResponse": "0_correct_answer[,]1_Rectangle_2",
      "ScormActivityId": "12392705",
      "InteractionIndex": "3",
      "AULMRID": "38093846"
    },
   {
      "ID": "ispring://presentations/CAA34147-7B48-40C6-84FD-5CE8077DB2BF/quizzes/",
      "Type": "2",
      "TimestampUtc": "12/8/2020 6:19:12 PM",
      "Timestamp": "12/8/2020 6:19:12 PM",
      "Weighting": "",
      "Result": "1",
      "Latency": "1300",
      "Description": "'Can't do' language tends to relay this impression...\n\nSelect one.",
      "LearnerResponse": "4_All_of_the_above",
      "ScormActivityId": "13334358",
      "InteractionIndex": "3",
      "AULMRID": "40715598"
    },
  ]
}

Any ideas on how to unify them?

Thank you!

RG

Try something like the following:

jq -n '{ Interactions: [ inputs.Interactions ] | add }' file1.json file2.json

This assumes that you have made both input files valid JSON by stripping that trailing comma at the end of each object in the Interactions array.

For input file1.json:

{
  "Interactions": [
    {
      "ID": "file1",
      "Type": "1",
      "Timestamp": "8/27/2020 11:11:11 PM"
    }
  ]
}

and input file2.json:

{
  "Interactions": [
    {
      "ID": "file2",
      "Type": "2",
      "Timestamp": "8/27/2020 22:22:22 PM"
    }
  ]
}

this results in:

{
  "Interactions": [
    {
      "ID": "file1",
      "Type": "1",
      "Timestamp": "8/27/2020 11:11:11 PM"
    },
    {
      "ID": "file2",
      "Type": "2",
      "Timestamp": "8/27/2020 22:22:22 PM"
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM