0

I am trying to convert the sample input below into the output below using jq:

Input JSON

  "elements": [
    {
      "type": "CustomObjectData",
      "id": "2185",
      "fieldValues": [
        {
          "type": "FieldValue",
          "id": "169",
          "value": "9/6/2017 12:00:00 AM"
        },
        {
          "type": "FieldValue",
          "id": "190",
          "value": "ABC"
        } 
        ]
        },
 {
      "type": "CustomObjectData",
      "id": "2186",
      "contactId": "13",
      "fieldValues": [
        {
          "type": "FieldValue",
          "id": "169",
          "value": "8/31/2017 12:00:00 AM"
        },
        {
          "type": "FieldValue",
          "id": "190",
          "value": "DEF"
        }
        ]
        }
]

Desired Output (group by id)

Essentially trying to extract "value" field from each "fieldValues" object and group them by "id"

{
"id:"2185",
"value": "9/6/2017 12:00:00 AM",
 "value": "ABC"
},
{
"id:"2186",
"value": "8/31/2017 12:00:00 AM",
 "value": "DEF"
}

What jq syntax should i use to achieve this? Thanks very much!!

3
  • First, it would seem the input is intended to be JSON, so if that's the case, please fix it. Second, the expected output is invalid as JSON, so it would be helpful to know what you really want, especially as the duplication of keys within each object is so problematic. Third, since SO is not a free programming service, questions on SO are usually accompanied by an illustration showing at least one attempt to solve the problem. Commented Aug 28, 2020 at 7:45
  • Ultimately I want to convert the input to CSV. the input I provided is only a snip of what I actually have, so I manually typed in some values and brackets. Might have made it look weird. Commented Aug 31, 2020 at 3:49
  • essentially I'd want the output to be grouped by id (2185 / 2186) and list the 'value' row, separated by comma. Commented Aug 31, 2020 at 3:50

1 Answer 1

1

Assuming the input shown in the Q has been modified in the obvious way to make it valid JSON, the following filter will produce the output as shown below, that is, a stream of valid JSON values that is similar to the allegedly expected output included in the Q. If a single array is desired, one possibility would be to wrap the program in square brackets.

program.jq

.elements[]
| {id, values: [ .fieldValues[].value] }

Output

{
  "id": "2185",
  "values": [
    "9/6/2017 12:00:00 AM",
    "ABC"
  ]
}
{
  "id": "2186",
  "values": [
    "8/31/2017 12:00:00 AM",
    "DEF"
  ]
}

Producing CSV

One of many possibilities:

.elements[]
| [.id] + [.fieldValues[].value]
| @csv

With the -r command-line option, this produces the following CSV:

"2185","9/6/2017 12:00:00 AM","ABC"
"2186","8/31/2017 12:00:00 AM","DEF"
Sign up to request clarification or add additional context in comments.

3 Comments

I am trying to convert this to csv. This works but when I try to convert it to csv it says 'object ({"id":"2185...) cannot be csv-formatted, only array'
Thank you very much! One last question - how would I go about adding an arbitrary header at the beginning?
The basic idea: (HEADER), (JQ_FILTER), e.g. "a\tb\tc", (... | @tsv)

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.