简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM