10

I have been able to get this far.

json input, 'data.json':

[
  {
    "Selected": null,
    "Family Name": "Jones",
    "Couple Name": "Jones, Adam & Rachael Margaret",
    "Family Phone": "404-4477",
    "Family Email": "[email protected]",
    "Family Address": "777 Aggies Court Kindly, California 95388 ",
    "Head Of House Name": "Jones, Adam",
    "Head Of House Phone": "(583) 404-2488",
    "Head Of House Email": "[email protected]",
    "Spouse Name": "Jones, Rachael Margaret",
    "Spouse Phone": null,
    "Spouse Email": null,
    "Child Name": null,
    "Child Phone": null,
    "Child Email": null
  },
  {
    "Selected": "x",
    "Family Name": "Xiong",
    "Couple Name": "Xiong, Arlene Frances",
    "Family Phone": null,
    "Family Email": null,
    "Family Address": "888 Walnut Ave. Blatant, California 95388 ",
    "Head Of House Name": "Xiong, Arlene Frances",
    "Head Of House Phone": "583-500-7917",
    "Head Of House Email": "[email protected]",
    "Spouse Name": null,
    "Spouse Phone": null,
    "Spouse Email": null,
    "Child Name": null,
    "Child Phone": null,
    "Child Email": null
  },
  {
    "Selected": "x",
    "Family Name": "Blair",
    "Couple Name": "Blair, Toby & Silvia",
    "Family Phone": "358-4645",
    "Family Email": null,
    "Family Address": "333 Cindy St. Stoic, California 95388 ",
    "Head Of House Name": "Blair, Toby",
    "Head Of House Phone": null,
    "Head Of House Email": "[email protected]",
    "Spouse Name": "Blair, Silvia",
    "Spouse Phone": null,
    "Spouse Email": null,
    "Child Name": null,
    "Child Phone": null,
    "Child Email": null
  }
]

I can use this to filter:

cat data.json | jq '.[] | select(.Selected != null) | {"Head Of House Name", "Head Of House Phone", "Head Of House Email", "Family Phone", "Family Email"}'

The results:

{
  "Head Of House Name": "Xiong, Arlene Frances",
  "Head Of House Phone": "583-500-7917",
  "Head Of House Email": "[email protected]",
  "Family Phone": null,
  "Family Email": null
}
{
  "Head Of House Name": "Blair, Toby",
  "Head Of House Phone": null,
  "Head Of House Email": "[email protected]",
  "Family Phone": "358-4645",
  "Family Email": null
}

Notice there are no array brackets around the array of objects and no comma between the objects... indicating the result is not an array.

The problem is that the result is not an array of objects (frankly, I'm not sure what it is). How can I start with an array of json objects, filter them and end up with a filtered list of objects?

1
  • Your question was helpful to me as an example of how to extract a specific JSON object from a JSON array, thank you very much for posting this! cat data.json | jq '.[] | select(.presetName == "presetOne")' > "presetOne.json" Commented Jan 18, 2019 at 2:34

1 Answer 1

16

When you use the filter .[], you are actually breaking the array result into separate results for each of the items. What you're seeing in the end is each of the individual results, one after the other.

To ensure they are kept as an array, you can put the results back into an array by wrapping your filter with square brackets:

[
    .[] |
    select(.Selected != null) |
    {
        "Head Of House Name",
        "Head Of House Phone",
        "Head Of House Email",
        "Family Phone",
        "Family Email"
    }
]

Or keep it as an array by using the map() filter, you could then remove the .[] part:

map(
    select(.Selected != null) |
    {
        "Head Of House Name",
        "Head Of House Phone",
        "Head Of House Email",
        "Family Phone",
        "Family Email"
    }
)

If you asked me, you should stick with map().

Sign up to request clarification or add additional context in comments.

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.