Search code examples
jsonstringjqfromjson

JQ how to read data from inner array with escaping


My json looks like:

{
    "response": {
        "resNum": 222222,
        "start": 0,
        "array": [{
            "tr_id": "xx33fg",
            "user_id": "6678",
            "x_date": "2021-04-27",
            "list": [
                "[{\"id\":\"123\",\"val2\":\"RX\",\"date\":\"2020-11-13\"}",
                "{\"id\":\"456\",\"val2\":\"DB\",\"date\":\"2020-09-20\"}]"
            ]
        }]
    }
}

And I need to convert it into this:

{
  "Result": [
    {
      "x_date": "2021-04-10",
      "array": [
        {
          "id": "345",
          "val2": "RX",
          "date": "2021-04-10"
        },
        {
          "id": "223",
          "val2": "XC",
          "date": "2021-04-10"
        }
      ]
    }
  ]
}

How can I read id, val2 and date from list using jq? I've tried

id: .response? | .array[]? | .list[]? | .id

but is was unsuccessfully and I get an error. I think nested array with escaping is the reason. Could someone help me? Thanks!


Solution

  • Use fromjson to unescape a string.

    jq '.response.array[]
        | {Result:[{x_date, array: .list
                    | join(",")
                    | fromjson}]}'
    

    The output is different to what you posted, but I have no idea how you wanted to translate the values.