0

I have two files, one is j.json and the other is x.xml, I want to fetch/query some data from both files. Is it possible to do this using regex? currently, I am using jq for querying data from JSON file. I was planning to use regex for Querying XML data,but then this above doubt came across my mind. It would be super easier for me if i use same library for both json and XML.

PS- i don't want to transform files, json-to-xml or vice versa.

Thank you.

user18148705
  • 215
  • 1
  • 11
  • XPath 3.1 can query both JSON and XML data, it is available with SaxonJS 2 for both client-side as well as server-side (node.js) JavaScript, as well as with Saxon HE for the Java and the .NET platform or with SaxonC for C/C++ with Python and PHP extensions. XQuery 3.1 is a superset of XPath 3.1 and can therefore query and manipulate both JSON and XML data and is implemented in databases like BaseX or eXist-db. – Martin Honnen Oct 10 '22 at 07:23
  • @MartinHonnen snap! – Conal Tuohy Oct 10 '22 at 07:25
  • @MartinHonnen hey thanks for the response! I just came across this library called xq, thinking of using it instead. – user18148705 Oct 10 '22 at 08:49

1 Answers1

0

For some simple XML and JSON documents it may be feasible, but you can't in general parse XML or JSON with regular expressions.

Why it's not possible to use regex to parse HTML/XML: a formal explanation in layman's terms

If you want to use the same technique or toolset to parse both data formats, you might want to look at XPath 3.1 as a query language: https://www.w3.org/TR/xpath-31/

Saxon is an interpreter you can use to execute XPath 3.1 queries: https://www.saxonica.com/documentation11/index.html#!xpath-api

Conal Tuohy
  • 2,561
  • 1
  • 8
  • 15