jsoncmdxidel

How to extract exact values from a json file with xidel?


Excuse my English, I am not a native speaker

I'm new to this so I don't know much

I am trying to extract some values from a json file with xidel with the following command in windows cmd but it's not working

xidel MyFile.json -e '$json//options/option/*[@option_id="D-ES"]/content_id'

Generally the json file has three options, English, Spanish and Portuguese, I only want all the values related to Spanish

I want to extract the following values

"group_id": "******",                                       
"content_id": "******",                                     
"current_content": "*****",                                     
"option_id": "D-ES",                                                                        
"subtitle": *****,                                                                              
"id": "ES",                                     
"desc": "Español",

And put the extracted values as follows

"group_id"-"*****","content_id"-"*****","current_content"-"*****","option_id"-"D-ES"-"subtitle"- *****,"id"- "ES""desc"- "Español",

This is part of my json file

{
  "original": {
    "id": "ING",
    "desc": "Inglés"
  },
  "dubbed": "true",
  "subbed": "false",
  "options": {
    "option": [
      {
        "group_id": "922450",
        "content_id": "284951",
        "current_content": "false",
        "option_id": "D-ES",
        "audio": "ES",
        "subtitle": null,
        "option_name": "dubbed",
        "id": "ES",
        "desc": "Español",
        "label_short": "Dob. Español",
        "label_large": "Doblada al Español",
        "intro_start_time": null,
        "intro_finish_time": null,
      },
      {
        "group_id": "275495",
        "content_id": "243856",
        "current_content": "false",
        "option_id": "D-PT",
        "audio": "PT",
        "subtitle": null,
        "option_name": "dubbed",
        "id": "PT",
        "desc": "Portugués",
        "label_short": "Dob. Portugués",
        "label_large": "Doblada al Portugués",
        "intro_start_time": null,
        "intro_finish_time": null,
      },
      {
        "group_id": "248954",
        "content_id": "245238",
        "current_content": "false",
        "option_id": "O-EN",
        "audio": "ORIGINAL",
        "subtitle": null,
        "option_name": "original",
        "id": "EN",
        "desc": "Inglés",
        "label_short": "Id. Inglés",
        "label_large": "Idioma Original Inglés",
        "intro_start_time": null,
        "intro_finish_time": null,
      }
    ]
  }
}

What command should I use to extract the values related to Spanish?


Solution

  • xidel MyFile.json -e '$json//options/option/*[@option_id="D-ES"]/content_id'
    

    So the correct query would be:

    -e "$json//options/(option)()[option_id='D-ES']/content_id"
    

    I want to extract the following values [...]

    C:\>xidel -s "MyFile.json" -e "$json//options/(option)()[option_id='D-ES']/(group_id,content_id,current_content,option_id,subtitle,id,desc)"
    922450
    284951
    false
    D-ES
    ES
    Español
    

    To include the attribute names and surrounding double quotes I would use a for-loop and an XPath 4.0 String Template:

    C:\>xidel -s "MyFile.json" -e "for $x in ('group_id','content_id','current_content','option_id','subtitle','id','desc') return $json//options/`\"{$x}\"-\"{(option)()[option_id='D-ES']($x)}\"`"
    "group_id"-"922450"
    "content_id"-"284951"
    "current_content"-"false"
    "option_id"-"D-ES"
    "subtitle"-""
    "id"-"ES"
    "desc"-"Español"
    

    And the simplest way to turn this sequence into a single line (where each item is separated by a ,) is to use --output-separator:

    C:\>xidel -s "MyFile.json" -e "for $x in ('group_id','content_id','current_content','option_id','subtitle','id','desc') return $json//options/`\"{$x}\"-\"{(option)()[option_id='D-ES']($x)}\"`" --output-separator=,
    C:\>xidel -s "MyFile.json" -e ^"^
      for $x in (^
        'group_id','content_id','current_content',^
        'option_id','subtitle','id','desc'^
      )^
      return^
      $json//options/`\"{$x}\"-\"{(option)()[option_id='D-ES']($x)}\"`^
    " --output-separator=,
    "group_id"-"922450","content_id"-"284951","current_content"-"false","option_id"-"D-ES","subtitle"-"","id"-"ES","desc"-"Español"