Home > Blockchain >  Parse a json data out from a tag
Parse a json data out from a tag

Time:02-08

The text below is part of a string I parsed from html (pre). Since I can not place tags <> in this box I have replace the beginning and end tags as (pre) (/pre).

(pre)(b)Below are the details for server SomeServerName from NetSystem.(Data Length - 1)(/b)
[
    {
        "askId": "Value1",
        "billingCode": "99999999",
        "clusterId": null,
        "createdBy": "Mike",
        "createdFromLegacy": null,
        "createdOn": "2021-08-06T17:54:28.220Z",
        "description": "Windows 2019",
        "environment": "devops",
        "hostId": null,
        "id": "acd16582-b009-4667-aa95-5977603772sa",
        "infrastructure": {
            "apiId": "App2019_SA_1_v8-w2019_mike3_cc8f7e02-d426-423d-addb-b29bc7e163be",
            "capacityId": "ODI",
            "catalogManagementGroup": "Sales Marketing"
        },
        "legacyId": "XL143036181",
        "location": {
            "availabilityZone": "ny",
            "code": "mx31",
            "description": "uhg : mn053",
            "region": "west",
            "vendor": "apple"
        },
        "maintenance": {
            "group": "3",
            "status": "steady_state",
            "window": {
                "days": "Sunday",
                "endTime": "06:00:00.000Z",
                "startTime": "02:00:00.000Z"
            }
        },
        "name": "SomeServer",
        "network": {
            "fqdn": "SomeServer.dom.tes.contoso.com",
            "ipAddress": "xx.xx.xx.xx"
        },
        "os": {
            "description": "Microsoft windows 2019",
            "type": "windows",
            "vendor": "Microsoft",
            "version": "2019"
        },
        "owner": {
            "id": "000111111",
            "msid": "jtest"
        },
        "provision": {
            "id": "ba424e42-a925-49a5-a4b7-5dcf41b69d4e",
            "requestingApi": "mars Hub",
            "system": "vRealize"
        },
        "specs": {
            "cpuCount": 4,
            "description": "Virtual Hardware",
            "ram": 64384,
            "serialNumber": null
        },
        "status": "ACTIVE",
        "support": {
            "group": "Support group"
        },
        "tags": {
            "appTag": "minitab"
        },
        "updatedBy": "snir_agent",
        "updatedOn": "2021-08-06T17:54:31.525Z"
    }
](/pre)

As you can see this is almost json data but I can not parse it as such because of the (b) (/b) tag that exists inside my (pre) (/pre) tag. How can I parse out this (b) tag with its content so I am left with the json data and can treat it as such enabling me to more easily select values with json functions.

CodePudding user response:

You can do this either by using re as indicated here or using split:

cleaned = data.split("(b)")[0]   data.split("(/b)")[1]

Above line will concatenate the content before (b) and after (/b) cleaning the b tag and its content.

CodePudding user response:

If your JSON always has [] brackets you can extract the content inside it and then parse it:

Python example:

import re
import json

text = '<b>asd</b>[{"a": "b", "c": "d"}] pre' # your content

json_content = re.findall('\[[^\]]*\]', text)[0] # '[{"a": "b", "c": "d"}]'
result = json.loads(json_content) # [{'a': 'b', 'c': 'd'}]
  •  Tags:  
  • Related