1

This task is similar to this one but in my case I would like to go other way around. So say we have input:

[
    {
        "name": "John",
        "email": "[email protected]"
    },
    {
        "name": "Brad",
        "email": "[email protected]"
    }
]

and desired output is:

{
    "name": "John",
    "email": "[email protected]"
}
{
    "name": "Brad",
    "email": "[email protected]"
}

I tried to write a bash function which will do it in loop:

#!/bin/bash

json=`cat $1`
length=`echo $json | jq '. | length'`

for (( i=0; i<$length ; i++ ))
do
echo $json | jq ".[$i]"
done

but it is obviously extremly slow...

Is there any way how to use jq better for this?

2 Answers 2

3

You can use this :

jq '.[]' file

If you use the .[index] syntax, but omit the index entirely, it will return all of the elements of an array.

Test:

$ jq '.[]' file
{
  "email": "[email protected]",
  "name": "John"
}
{
  "email": "[email protected]",
  "name": "Brad"
}
Sign up to request clarification or add additional context in comments.

Comments

2

you can apply ".[]" filter.

This tutorial is very informative https://stedolan.github.io/jq/tutorial/

Comments

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.