2

With the following input example:

[
  {
    "orgid": "1",
    "email": "[email protected]",
    "first_name": "Jane",
    "last_name": "Doe",
    "phone": "4045551212",
    "tags": [
      "tag1",
      "tag2"
    ],
    "fields": [
      {
        "id": "2",
        "value": "IB4220"
      },
      {
        "id": "3",
        "value": "1994-05-18"
      },
      {
        "id": "12",
        "value": "CONSULTANT"
      },
      {
        "id": "13",
        "value": "[email protected]"
      },
      {
        "id": "6",
        "value": "111 Main Blvd"
      },
      {
        "id": "7",
        "value": "Babylon"
      },
      {
        "id": "8",
        "value": "NY"
      },
      {
        "id": "9",
        "value": "11702-1522"
      },
      {
        "id": "10",
        "value": "US"
      },
      {
        "id": "11",
        "value": ""
      },
      {
        "id": "5",
        "value": "http://www.something.com/janedoe"
      }
    ]
  }
]

I am trying to convert the "id" field to a number using jq's "tonumber" function.

Here is my jq filter I am working with to no avail...

.[] | {contacts:[.[] | {orgid: .["orgid"],email: .["email"],first_name: .["first_name"],last_name: .["last_name"],phone: .["phone"],tags: .["tags"],fields: [{id: .fields[].id|tonumber, value: .fields[].value}]}]}

It converts the field to a number; but yields the following undesired result (the repetition of the record many times.)

{
  "contacts": [
    {
      "orgid": "1",
      "email": "[email protected]",
      "first_name": "Jane",
      "last_name": "Doe",
      "phone": "4045551212",
      "tags": [
        "tag1",
        "tag2"
      ],
      "fields": [
        {
          "id": 2,
          "value": "IB4220"
        },
        {
          "id": 2,
          "value": "1994-05-18"
        },
        {
          "id": 2,
          "value": "CONSULTANT"
        },
        {
          "id": 2,
          "value": "[email protected]"
        },
        {
          "id": 2,
          "value": "111 Main Blvd"
        },
        {
          "id": 2,
          "value": "Babylon"
        },
        {
          "id": 2,
          "value": "NY"
        },
        {
          "id": 2,
          "value": "11702-1522"
        },
        {
          "id": 2,
          "value": "US"
        },
        {
          "id": 2,
          "value": ""
        },
        {
          "id": 2,
          "value": "http://www.something.com/janedoe"
        },
        {
          "id": 3,
          "value": "IB4220"
        },
        {
          "id": 3,
          "value": "1994-05-18"
        },
        {
          "id": 3,
          "value": "CONSULTANT"
        },
        {
          "id": 3,
          "value": "[email protected]"
        },
        {
          "id": 3,
          "value": "111 Main Blvd"
        },
        {
          "id": 3,
          "value": "Babylon"
        },
        {
          "id": 3,
          "value": "NY"
        },
        {
          "id": 3,
          "value": "11702-1522"
        },
        {
          "id": 3,
          "value": "US"
        },
        {
          "id": 3,
          "value": ""
        },
        {
          "id": 3,
          "value": "http://www.something.com/janedoe"
        },
        {
          "id": 12,
          "value": "IB4220"
        },
        {
          "id": 12,
          "value": "1994-05-18"
        },
        {
          "id": 12,
          "value": "CONSULTANT"
        },
        {
          "id": 12,
          "value": "[email protected]"
        },
        {
          "id": 12,
          "value": "111 Main Blvd"
        },
        {
          "id": 12,
          "value": "Babylon"
        },
        {
          "id": 12,
          "value": "NY"
        },
        {
          "id": 12,
          "value": "11702-1522"
        },
        {
          "id": 12,
          "value": "US"
        },
        {
          "id": 12,
          "value": ""
        },
        {
          "id": 12,
          "value": "http://www.something.com/janedoe"
        },
        {
          "id": 13,
          "value": "IB4220"
        },
        {
          "id": 13,
          "value": "1994-05-18"
        },
        {
          "id": 13,
          "value": "CONSULTANT"
        },
        {
          "id": 13,
          "value": "[email protected]"
        },
        {
          "id": 13,
          "value": "111 Main Blvd"
        },
        {
          "id": 13,
          "value": "Babylon"
        },
        {
          "id": 13,
          "value": "NY"
        },
        {
          "id": 13,
          "value": "11702-1522"
        },
        {
          "id": 13,
          "value": "US"
        },
        {
          "id": 13,
          "value": ""
        },
        {
          "id": 13,
          "value": "http://www.something.com/janedoe"
        },
        {
          "id": 6,
          "value": "IB4220"
        },
        {
          "id": 6,
          "value": "1994-05-18"
        },
        {
          "id": 6,
          "value": "CONSULTANT"
        },
        {
          "id": 6,
          "value": "[email protected]"
        },
        {
          "id": 6,
          "value": "111 Main Blvd"
        },
        {
          "id": 6,
          "value": "Babylon"
        },
        {
          "id": 6,
          "value": "NY"
        },
        {
          "id": 6,
          "value": "11702-1522"
        },
        {
          "id": 6,
          "value": "US"
        },
        {
          "id": 6,
          "value": ""
        },
        {
          "id": 6,
          "value": "http://www.something.com/janedoe"
        },
        {
          "id": 7,
          "value": "IB4220"
        },
        {
          "id": 7,
          "value": "1994-05-18"
        },
        {
          "id": 7,
          "value": "CONSULTANT"
        },
        {
          "id": 7,
          "value": "[email protected]"
        },
        {
          "id": 7,
          "value": "111 Main Blvd"
        },
        {
          "id": 7,
          "value": "Babylon"
        },
        {
          "id": 7,
          "value": "NY"
        },
        {
          "id": 7,
          "value": "11702-1522"
        },
        {
          "id": 7,
          "value": "US"
        },
        {
          "id": 7,
          "value": ""
        },
        {
          "id": 7,
          "value": "http://www.something.com/janedoe"
        },
        {
          "id": 8,
          "value": "IB4220"
        },
        {
          "id": 8,
          "value": "1994-05-18"
        },
        {
          "id": 8,
          "value": "CONSULTANT"
        },
        {
          "id": 8,
          "value": "[email protected]"
        },
        {
          "id": 8,
          "value": "111 Main Blvd"
        },
        {
          "id": 8,
          "value": "Babylon"
        },
        {
          "id": 8,
          "value": "NY"
        },
        {
          "id": 8,
          "value": "11702-1522"
        },
        {
          "id": 8,
          "value": "US"
        },
        {
          "id": 8,
          "value": ""
        },
        {
          "id": 8,
          "value": "http://www.something.com/janedoe"
        },
        {
          "id": 9,
          "value": "IB4220"
        },
        {
          "id": 9,
          "value": "1994-05-18"
        },
        {
          "id": 9,
          "value": "CONSULTANT"
        },
        {
          "id": 9,
          "value": "[email protected]"
        },
        {
          "id": 9,
          "value": "111 Main Blvd"
        },
        {
          "id": 9,
          "value": "Babylon"
        },
        {
          "id": 9,
          "value": "NY"
        },
        {
          "id": 9,
          "value": "11702-1522"
        },
        {
          "id": 9,
          "value": "US"
        },
        {
          "id": 9,
          "value": ""
        },
        {
          "id": 9,
          "value": "http://www.something.com/janedoe"
        },
        {
          "id": 10,
          "value": "IB4220"
        },
        {
          "id": 10,
          "value": "1994-05-18"
        },
        {
          "id": 10,
          "value": "CONSULTANT"
        },
        {
          "id": 10,
          "value": "[email protected]"
        },
        {
          "id": 10,
          "value": "111 Main Blvd"
        },
        {
          "id": 10,
          "value": "Babylon"
        },
        {
          "id": 10,
          "value": "NY"
        },
        {
          "id": 10,
          "value": "11702-1522"
        },
        {
          "id": 10,
          "value": "US"
        },
        {
          "id": 10,
          "value": ""
        },
        {
          "id": 10,
          "value": "http://www.something.com/janedoe"
        },
        {
          "id": 11,
          "value": "IB4220"
        },
        {
          "id": 11,
          "value": "1994-05-18"
        },
        {
          "id": 11,
          "value": "CONSULTANT"
        },
        {
          "id": 11,
          "value": "[email protected]"
        },
        {
          "id": 11,
          "value": "111 Main Blvd"
        },
        {
          "id": 11,
          "value": "Babylon"
        },
        {
          "id": 11,
          "value": "NY"
        },
        {
          "id": 11,
          "value": "11702-1522"
        },
        {
          "id": 11,
          "value": "US"
        },
        {
          "id": 11,
          "value": ""
        },
        {
          "id": 11,
          "value": "http://www.something.com/janedoe"
        },
        {
          "id": 5,
          "value": "IB4220"
        },
        {
          "id": 5,
          "value": "1994-05-18"
        },
        {
          "id": 5,
          "value": "CONSULTANT"
        },
        {
          "id": 5,
          "value": "[email protected]"
        },
        {
          "id": 5,
          "value": "111 Main Blvd"
        },
        {
          "id": 5,
          "value": "Babylon"
        },
        {
          "id": 5,
          "value": "NY"
        },
        {
          "id": 5,
          "value": "11702-1522"
        },
        {
          "id": 5,
          "value": "US"
        },
        {
          "id": 5,
          "value": ""
        },
        {
          "id": 5,
          "value": "http://www.something.com/janedoe"
        }
      ]
    }
  ]
}

Desired result:

[
  {
    "orgid": "1",
    "email": "[email protected]",
    "first_name": "Jane",
    "last_name": "Doe",
    "phone": "4045551212",
    "tags": [
      "tag1",
      "tag2"
    ],
    "fields": [
      {
        "id": 2,
        "value": "IB4220"
      },
      {
        "id": 3,
        "value": "1994-05-18"
      },
      {
        "id": 12,
        "value": "CONSULTANT"
      },
      {
        "id": 13,
        "value": "[email protected]"
      },
      {
        "id": 6,
        "value": "111 Main Blvd"
      },
      {
        "id": 7,
        "value": "Babylon"
      },
      {
        "id": 8,
        "value": "NY"
      },
      {
        "id": 9,
        "value": "11702-1522"
      },
      {
        "id": 10,
        "value": "US"
      },
      {
        "id": 11,
        "value": ""
      },
      {
        "id": 5,
        "value": "http://www.something.com/janedoe"
      }
    ]
  }
]

1 Answer 1

7

This yields your expected output:

.[].fields[].id |= tonumber

|= takes the paths .[].fields[].id expands to and applies tonumber to their values.

demo at jqplay.org

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.