Search code examples
jsonxidel

How to read correctly this JSON file with Xidel?


Excuse my English, I am not a native speaker

I have a json file "VideoJson.json" which contains the following

VideoJSONLoaded({"video_type": "0","image_id": "0","profile": false,"published_urls": [{"embed_url": "https://alura.hls/vod/p/manifest/55.mpd","protocol": "https","cdn_name": "Cloud","state": 10,"live_url": "https://alura.hls/vod/p/manifest/55.mpd"}],"access_rules": "{}","timed_cues": [],"embedded_cc": 1,"adst_temp_ver": "2"})

I try to read the json with Xidel with the following command

xidel-0.9.8.x32 "VideoJson.json" -e "$json"

And I get an error message

Error: jerr:JNDY0021: error at VideoJSONLoaded (tkIdentifier) in VideoJSONLoaded...

I think it's because the json is inside VideoJSONLoaded(JSON)

What command should I use to be able to read the json correctly and be able to extract data?


Solution

  • Whatever tool provided you this file did a bad job, because it's not JSON at all.

    When you open a '*.json' file, xidel assumes JSON (--input-format=json). If it's not, then you have to override it with --input-format=text (--input-format=html for v0.9.8):

    xidel -s --input-format=text "VideoJson.json" -e "$raw"
    VideoJSONLoaded({"video_type": "0",[...],"adst_temp_ver": "2"})
    

    To extract the JSON you could use substring-before() and substring-after():

    xidel -s --input-format=text "VideoJson.json" -e "substring-before(substring-after($raw,'VideoJSONLoaded('),')')"
    {"video_type": "0",[...],"adst_temp_ver": "2"}
    

    Or extract():

    xidel -s --input-format=text "VideoJson.json" -e "extract($raw,'\{.+\}')"
    {"video_type": "0",[...],"adst_temp_ver": "2"}
    

    And finally parse-json() (json() for v0.9.8) to parse the JSON:

    xidel -s --input-format=text "VideoJson.json" -e "parse-json(extract($raw,'\{.+\}'))"
    {
      "video_type": "0",
      "image_id": "0",
      "profile": false,
      "published_urls": [
        {
          "embed_url": "https://alura.hls/vod/p/manifest/55.mpd",
          "protocol": "https",
          "cdn_name": "Cloud",
          "state": 10,
          "live_url": "https://alura.hls/vod/p/manifest/55.mpd"
        }
      ],
      "access_rules": "{}",
      "timed_cues": [],
      "embedded_cc": 1,
      "adst_temp_ver": "2"
    }