Home > Software engineering >  It's possible to find a link in a object if I know how it starts?
It's possible to find a link in a object if I know how it starts?

Time:11-23

I'm getting a response from an API, I'm interested only in one of the link that returns. I have been using for a while

["data"]["tasks"]["0"]["result"]["files"]["0"]["url"] 

to get the URL but it seems the API has changed and now the "result" it changes the position 0 in every call...

{"data": {
  "id": "dcd99049-63b2",
  "tag": "jobbuilder",
  "status": "finished",
  "created_at": "2022-11-22T21:57:28 00:00",
  "started_at": "2022-11-22T21:57:28 00:00",
  "ended_at": "2022-11-22T21:57:33 00:00",
  "tasks": [
    {
      "id": "48c34a03-54f2",
      "name": "task-2",
      "job_id": "dcd99049-63b2",
      "status": "finished",
      "credits": 0,
      "code": null,
      "message": null,
      "percent": 100,
      "operation": "convert",
      "engine": "qpdf",
      "engine_version": "10.1.0",
      "result": {
        "files": [
          {
            "filename": "something.pdf",
            "size": 633487
                        }
        ]
      },
      "created_at": "2022-11-22T21:57:30 00:00",
      "started_at": "2022-11-22T21:57:32 00:00",
      "ended_at": "2022-11-22T21:57:32 00:00",
      "retry_of_task_id": null,
      "copy_of_task_id": "dd64385a-a2e9",
      "user_id": 6070,
      "priority": 10,
      "host_name": "meggie",
      "storage": null,
      "depends_on_task_ids": [
                    "dd64385a-a2e9",
      "acfd2a2c-3c45"
                ],
  "links": {
    "self": "https://api.cloudconvert.com/v2/tasks/48c34a03-54f2"
                }
},
  {
    "id": "6333e3b8-2f6f",
    "name": "export-1",
    "job_id": "dcd99049-63b2",
    "status": "finished",
    "credits": 0,
    "code": null,
    "message": null,
    "percent": 100,
    "operation": "export/url",
    "result": {
      "files": [
        {
          "filename": "something.pdf",
          "size": 633487,
          "url": "https://storage.cloudconvert.com/tasks/somethinglink.pdf"
                        }
      ]
    },
    "created_at": "2022-11-22T21:57:28 00:00",
    "started_at": "2022-11-22T21:57:32 00:00",
    "ended_at": "2022-11-22T21:57:32 00:00",
    "retry_of_task_id": null,
    "copy_of_task_id": null,
    "user_id": 607,
    "priority": 10,
    "host_name": "meggie",
    "storage": "ceph-fra",
    "depends_on_task_ids": [
                    "48c34a03-54f2"
                ],
"links": {
  "self": "https://api.cloudconvert.com/v2/tasks/6333e3b8-2f6f"
}
            },
{
  "id": "dd64385a-a2e9",
    "name": "task-2-create-watermark",
      "job_id": "dcd99049-63b2",
        "status": "finished",
          "credits": 1,
            "code": null,
              "message": null,
                "percent": 100,
                  "operation": "watermark",
                    "engine": "imagemagick",
                      "engine_version": "7.1.0",
                        "result": {
    "files": [
      {
        "filename": "watermark.pdf",
        "size": 609229
      }
    ]
  },
  "created_at": "2022-11-22T21:57:28 00:00",
    "started_at": "2022-11-22T21:57:30 00:00",
      "ended_at": "2022-11-22T21:57:32 00:00",
        "retry_of_task_id": null,
          "copy_of_task_id": null,
            "user_id": 6070,
              "priority": 10,
                "host_name": "meggie",
                  "storage": "ceph-fra",
                    "depends_on_task_ids": [
                      "acfd2a2c-3c45-4e47"
                    ],
                      "links": {
    "self": "https://api.cloudconvert.com/v2/tasks/dd64385a-a2e9"
  }
},
{
  "id": "acfd2a2c-3c45-4e47",
    "name": "task-1",
      "job_id": "dcd99049-63b2",
        "status": "finished",
          "credits": 1,
            "code": null,
              "message": null,
                "percent": 100,
                  "operation": "capture-website",
                    "engine": "wkhtml",
                      "engine_version": "0.12.5",
                        "result": {
    "files": [
      {
        "filename": "something.pdf",
        "size": 27330
      }
    ]
  },
  "created_at": "2022-11-22T21:57:28 00:00",
    "started_at": "2022-11-22T21:57:28 00:00",
      "ended_at": "2022-11-22T21:57:30 00:00",
        "retry_of_task_id": null,
          "copy_of_task_id": null,
            "user_id": 6070,
              "priority": 10,
                "host_name": "meggie",
                  "storage": null,
                    "depends_on_task_ids": [],
                      "links": {
    "self": "https://api.cloudconvert.com/v2/tasks/acfd2a2c-3c45"
  }
}
        ],
"links": {
  "self": "https://api.cloudconvert.com/v2/jobs/dcd99049-63b2-443e"
}
    }};

I would like to always extract only the link:

https://storage.cloudconvert.com/tasks/somethinglink.pdf 

which position can change as mentioned before... The link always start with https://storage.cloudconvert.com

I have been using this for a while to access the first object in the array

["data"]["tasks"]["0"]["result"]["files"]["0"]["url"] 

but it doesn't seem to work if the link it's in a different position...

CodePudding user response:

This code extracts all values as strings, then finds the one with the necessary prefix:

const data = {"data":{"id":"dcd99049-63b2","tag":"jobbuilder","status":"finished","created_at":"2022-11-22T21:57:28 00:00","started_at":"2022-11-22T21:57:28 00:00","ended_at":"2022-11-22T21:57:33 00:00","tasks":[{"id":"48c34a03-54f2","name":"task-2","job_id":"dcd99049-63b2","status":"finished","credits":0,"code":null,"message":null,"percent":100,"operation":"convert","engine":"qpdf","engine_version":"10.1.0","result":{"files":[{"filename":"something.pdf","size":633487}]},"created_at":"2022-11-22T21:57:30 00:00","started_at":"2022-11-22T21:57:32 00:00","ended_at":"2022-11-22T21:57:32 00:00","retry_of_task_id":null,"copy_of_task_id":"dd64385a-a2e9","user_id":6070,"priority":10,"host_name":"meggie","storage":null,"depends_on_task_ids":["dd64385a-a2e9","acfd2a2c-3c45"],"links":{"self":"https://api.cloudconvert.com/v2/tasks/48c34a03-54f2"}},{"id":"6333e3b8-2f6f","name":"export-1","job_id":"dcd99049-63b2","status":"finished","credits":0,"code":null,"message":null,"percent":100,"operation":"export/url","result":{"files":[{"filename":"something.pdf","size":633487,"url":"https://storage.cloudconvert.com/tasks/somethinglink.pdf"}]},"created_at":"2022-11-22T21:57:28 00:00","started_at":"2022-11-22T21:57:32 00:00","ended_at":"2022-11-22T21:57:32 00:00","retry_of_task_id":null,"copy_of_task_id":null,"user_id":607,"priority":10,"host_name":"meggie","storage":"ceph-fra","depends_on_task_ids":["48c34a03-54f2"],"links":{"self":"https://api.cloudconvert.com/v2/tasks/6333e3b8-2f6f"}},{"id":"dd64385a-a2e9","name":"task-2-create-watermark","job_id":"dcd99049-63b2","status":"finished","credits":1,"code":null,"message":null,"percent":100,"operation":"watermark","engine":"imagemagick","engine_version":"7.1.0","result":{"files":[{"filename":"watermark.pdf","size":609229}]},"created_at":"2022-11-22T21:57:28 00:00","started_at":"2022-11-22T21:57:30 00:00","ended_at":"2022-11-22T21:57:32 00:00","retry_of_task_id":null,"copy_of_task_id":null,"user_id":6070,"priority":10,"host_name":"meggie","storage":"ceph-fra","depends_on_task_ids":["acfd2a2c-3c45-4e47"],"links":{"self":"https://api.cloudconvert.com/v2/tasks/dd64385a-a2e9"}},{"id":"acfd2a2c-3c45-4e47","name":"task-1","job_id":"dcd99049-63b2","status":"finished","credits":1,"code":null,"message":null,"percent":100,"operation":"capture-website","engine":"wkhtml","engine_version":"0.12.5","result":{"files":[{"filename":"something.pdf","size":27330}]},"created_at":"2022-11-22T21:57:28 00:00","started_at":"2022-11-22T21:57:28 00:00","ended_at":"2022-11-22T21:57:30 00:00","retry_of_task_id":null,"copy_of_task_id":null,"user_id":6070,"priority":10,"host_name":"meggie","storage":null,"depends_on_task_ids":[],"links":{"self":"https://api.cloudconvert.com/v2/tasks/acfd2a2c-3c45"}}],"links":{"self":"https://api.cloudconvert.com/v2/jobs/dcd99049-63b2-443e"}}}

const f=i=>i instanceof Object?Object.values(i).flatMap(v=>f(v)):[i '']

console.log(f(data).find(i=>
  i.startsWith('https://storage.cloudconvert.com/tasks/')))

  • Related