Split a json array to multiple files

Daniel Kaplan

I have a file that looks like this:

[
  {
    "billingAccountNumber": "x",
    "paymentResponseObject": {
      "uiErrorDipslayMessage": "",
      "transactionStatus": "S",
      "transactionDescription": "",
      "transactionCode": "",
      "confirmationNumber": "1"
    }
  },
  {
    "billingAccountNumber": "y",
    "paymentResponseObject": {
      "uiErrorDipslayMessage": "",
      "transactionStatus": "S",
      "transactionDescription": "",
      "transactionCode": "",
      "confirmationNumber": "2"
    }
  },
  {
    "billingAccountNumber": "z",
    "paymentResponseObject": {
      "uiErrorDipslayMessage": "",
      "transactionStatus": "S",
      "transactionDescription": "",
      "transactionCode": "",
      "confirmationNumber": "3"
    }
  }
]

The data doesn't look exactly like this, and I have more than three elements. From this data, I want to create three files: x.json, y.json, and z.json. I want the contents of each of those files to be the contents of the paymentResponseObject.

Is there a way to do this with jq? I've figured out how to do this in awk, but it's very clunky and I want to be able to repeat this process with different schemas. I have to rewrite 80% of the awk script for each schema.

jesse_b

From this SO thread:

jq -cr 'keys[] as $k | "\($k)\n\(.[$k])"' input.json | while read -r key; do
  fname=$(jq --raw-output ".[$key].billingAccountNumber" input.json)
  read -r item
  printf '%s\n' "$item" > "./$fname"
done

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Split JSON into multiple variables/ an array

From Dev

How to split a module into multiple files

From Dev

Split file into multiple files in Excel

From Dev

split file into multiple files (by columns)

From Dev

Split multiple CSV files in batch

From Dev

Julia: Split module into multiple files

From Dev

Python Split files into multiple smaller files

From Dev

Split a JSON file into separate files

From Dev

How to split a file to multiple files with multiple threads?

From Dev

split and sort array into multiple arrays

From Dev

Split KQL array into multiple columns

From Dev

split and sort array into multiple arrays

From Dev

Validating multiple files in array

From Dev

Validating multiple files in array

From Dev

Load multiple JSON files

From Dev

How to split json array into strings?

From Dev

how to split JSON array in php

From Dev

How to split a JSON object into an array

From Dev

Split Array into multiple Array and store in different variables

From Dev

split element of array in multiple array bash

From Dev

ClojureScript split one namespace into multiple files

From Dev

How to split react js components into multiple files

From Dev

Java - Read file and split into multiple files

From Dev

split views.py into multiple files

From Dev

Split aggregated data table into multiple CSV files

From Dev

Split diary file into multiple files using Python

From Dev

How to split csv file into multiple files by size

From Dev

split routes in multiple files for play framework 2.2

From Dev

vba - Split Excel Worksheet into multiple files