0

I am new to Bash scripting and jq. I am trying to extract the key value pairs name and transcription.normalized from a JSON object.

I have learned how to get a list of all the values from name and normalized separately but it is not really what I am looking for.

cat submission.json | jq '.documents[] .pages[] .fields[] .name, .documents[] .pages[] .fields[] .transcription.normalized'

I am wondering if I need to perform some sort of loop but just not sure. I really want a single script that pulls those 2 fields in a format that I can easily dump to a CSV file.

This is the example of what the JSON looks like.

{
  "id": 1,
  "state": "complete",
  "substate": null,
  "exceptions": [],
  "name": "Sender Account Number",
  "output_name": null,
  "field_definition_attributes": {
    "required": false,
    "data_type": "Account Number",
    "multiline": false,
    "consensus_required": false,
    "supervision_override": null
  },
  "transcription": {
    "raw": "1685-0441-1",
    "normalized": "168504411",
    "source": "machine_transcription",
    "data_deleted": false,
    "user_transcribed": null,
    "row_index": null
  },
  "field_image_url": "/api/v4/image/be167a88-9d1d-43bc-82b2-3d96d8c06656?start_x=0.3110429607297866&start_y=0.1052441592299208&end_x=0.5696909842243418&end_y=0.16043316955780607"
}
0

1 Answer 1

1

You don't need a loop. And jq can produce CSV out of arrays.

jq -r '.documents[].pages[].fields[] | [.name, .transcription.normalized] | @csv' file
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, wow this is much easier than I was making it out to be then. Quick question, in your example, is "file" a placeholder for an existing file in my folder? Your code worked when I formatted it like this: cat submission.json | jq -r '.documents[].pages[].fields[] | [.name, .transcription.normalized] | @csv' > name.csv
@wahiggins3 yes, it is a placeholder. jq can read files on its own, cat file | jq is a useless use of cat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.