News for hackers. Stuff that matters.
Re:How to Use JSON Path by wodenokoto on Friday May 03, 2024 @05:40AM (#40245811)
Is there a general name for the kind of data structure JSON represents?

We see this kind of Nete’s data all over the place (json, yaml, python dictionaries, toml, etc, etc) and I’m thinking wouldn’t it be nice if we had a path language that worked across these structures, just like how we can regex any strings?

So we can have a pathql executable that we can feed yaml and json data to, but I can reuse the query in Python when I want to extract values from a json stream I just deserialized.

Re:How to Use JSON Path by crabmusket on Friday May 03, 2024 @06:26AM (#40246099)
I feel like `fq` has a query path language that's kind of generic across lots of file types. It can be fairly verbose for that reason. I was using it to debug MsgPack documents and it was a lot less intuitive than just using some dotted string paths with `jq`.

https://github.com/wader/fq/

Re:How to Use JSON Path by wwader on Friday May 03, 2024 @09:30AM (#40247467)
Hey, fq author here. Happy to hear it's useful! could you elaborate a bit more how it was less intuitive? fq's query language is jq with some small additions so i wonder if you might mean the decoded structure is more detailed/verbose as it includes all the "low level" details? maybe your looking for the "torepr" function that converts the detailed structure into the "represented" value?
Re:How to Use JSON Path by ranger_danger on Friday May 03, 2024 @10:43AM (#40248259)
Never seen this tool before but it looks quite handy. Thanks!
Re:How to Use JSON Path by wwader on Friday May 03, 2024 @02:54PM (#40250974)
hope it can be of use!
Re:How to Use JSON Path by crabmusket on Friday May 03, 2024 @07:28PM (#40253444)
Yes that's exactly what I meant, the MsgPack documents had quite a detailed structure.

torepr didn't quite work for me as I was dealing with objects containing large binary blobs and it was awkward.

fq is a great tool and I shouldn't have suggested this was a problem unique to it! I think this kind of "issue" is inevitable when dealing with so many types of input. And to be honest I struggle hard using jq as well for anything other than very basic paths, due to infrequent usage.

Re:How to Use JSON Path by wwader on Saturday May 04, 2024 @11:42AM (#40258454)
I see, thanks for replying and no worries! yeap some of the "self-describing" formats like msgpack, cbor etc will because of how fq works have to be decoded into something more of a meta-msgpack etc.

About blobs, if you want to change how (possibly large) binaries are represented as JSON you can use the bits_format options, see https://github.com/wader/fq/blob/master/doc/usage.md#options, so fq -o bits_format=md5 torepr ...

I can highly recommend to learn jq, it's what makes fq really useful, and as a bonus you will learn jq in general! :)

Re:How to Use JSON Path by crabmusket on Saturday May 04, 2024 @06:53PM (#40260960)
Oh that's a great tip, I will try to remember this next time I'm touching the msgpacky parts of my app!
Re:How to Use JSON Path by sesm on Friday May 03, 2024 @07:31AM (#40246453)
'Nested maps and arrays of strings, numbers and booleans'? I propose the name AMofBNS :)
Re:How to Use JSON Path by lucianbr on Friday May 03, 2024 @05:46AM (#40245839)
Is it anything else than a tree with properties for each node?

I think you could well apply jsonpath to yaml, except for the different data types, which is what makes you need xmlpath, jsonpath, file path, css and so on. If you're willing to do some automagic conversions, you could probably do that right now, if you write the code for it.

Re:How to Use JSON Path by exceptione on Friday May 03, 2024 @06:10AM (#40245994)
A tree with properties indeed.

They vary in syntax and how they deal with scalars, objects and collections. Having to write 'myKey' is a bit unfortunate in json for example. Still, for any document larger than 20 lines yaml will fall apart easily.

Xml (dialects) mark the beginning and end of nodes explicitly, which deviates from json/yaml. Xml can represent nodes within the value, which is impossible for json/yaml. To convert such xml value to the latter, you have to break up such a node in fragments and represent them as a collection in json/yaml.

Re:How to Use JSON Path by tsimionescu on Friday May 03, 2024 @09:58AM (#40247733)
> Xml can represent nodes within the value, which is impossible for json/yaml. To convert such xml value to the latter, you have to break up such a node in fragments and represent them as a collection in json/yaml.

Are you talking about XML like `<text>Something something <para> inside </para> something else</text>`? I thought this would also presented as the text element having three children, the text "Something something", the <para> tag with its subtree, and the text "something else". Am I misremembering?

Re:How to Use JSON Path by exceptione on Friday May 03, 2024 @01:07PM (#40249885)
Exactly, that is the «breaking apart» I was talking about. Xml is the more natural way for humans to express that particular use case.
Re:How to Use JSON Path by tsimionescu on Friday May 03, 2024 @01:57PM (#40250410)
But my point is that an XML parser would still present that as three children, right?

It's just when writing the document that you get the advantage, the data model is the same.

Re:How to Use JSON Path by exceptione on Friday May 03, 2024 @02:29PM (#40250740)
Jup, it is human ergonomics, but a mismatch for your average programming data structure.

I think that is one reason why json took of for exchanging machine readable data, xml is too expressive, and leans towards document authoring.

Re:How to Use JSON Path by tsimionescu on Friday May 03, 2024 @02:42PM (#40250863)
Agreed, a lot of the features that make XML very nice for embedding tags in text documents make it very inefficient at expressing explicit tree data structures.
Re:How to Use JSON Path by rileymat2 on Friday May 03, 2024 @06:44PM (#40253182)
I doubt that, if I had my money on it I’d guess the main reason is var obj=JSON.parse(string) and its ease.
Re:How to Use JSON Path by exceptione on Friday May 03, 2024 @07:38PM (#40253495)
That is half the reason. JSON was limited to Javascript when SOAP/WSDL was king for data exchange. If I remember correctly, XML apis predates json in Javascript. IE5 and XML were really hot back then. xml.loadString() is equally simple.

Json got introduced because XMLHttpRequest was an IE-only component. Data exchange was done mostly between servers, back then the front-end was really dumb. Why xml got slowly replaced is because that for machine generated data, json proved to be sufficient. Any json library is easier to work with then their xml counterpart, exactly because of what we discuss here.

Re:How to Use JSON Path by PurpleRamen on Friday May 03, 2024 @09:47AM (#40247613)
The complicated part is, that this tree has different types of nodes. Some support properties and child's, some not. So I would call it a chimera tree or mixed tree, or something like that.
Re:How to Use JSON Path by lucianbr on Friday May 03, 2024 @11:09AM (#40248556)
For a query language, you can generalize to all nodes supporting all things. You will be able to write some queries that will never return results, at least in a given input data format. That does not seem so bad.
Re:How to Use JSON Path by hughesjj on Friday May 03, 2024 @03:44PM (#40251537)
No it makes so much more sense to introduce nil and then constantly pepper your code with ? (Null-colasecing) Operators everywhere

(Looking at you, jq, though to be fair JSON itself supports null...)

Re:How to Use JSON Path by int_19h on Friday May 03, 2024 @07:49PM (#40253567)
Coincidentally, this is exactly what XPath and XQuery do. E.g. text nodes don't have children nor attributes, yet it is perfectly legal to do text()/foo or text()/@foo - and the result is an empty sequence.

If you want this kind of semantics with JSON, take a look at https://www.jsoniq.org

Re:How to Use JSON Path by itronitron on Friday May 03, 2024 @07:17AM (#40246366)
You could say it is 'object-oriented' since the ON stands for Object Notation.
Re:How to Use JSON Path by froh on Friday May 03, 2024 @08:10AM (#40246760)
nested records and arrays?
Re:How to Use JSON Path by qazxcvbnm on Friday May 03, 2024 @08:22AM (#40246859)
As to cross format path querying, I see limited value in such an endeavour; the reason that one deals with such formats is generally because it is used as an interface to configure some tool, but moving across different tools, besides the path queries, even the configuration schemas differ, and having a cross format converter would do you no good. The fact that many such configuration formats support subtly (or drastically) divergent types of objects does this idea no good either.

Speaking of this topic, I would be very interested in a CLI tool that translated between different dialects of regex. As opposed to general data or configuration, I find the case for cross format conversion very compelling for regexes; the objects that different regex languages deal with are functionally completely identical. I would be very happy to be able to craft a regex to search for something in vim, then convert the regex to grep or use another tool on the shell, then perhaps adapt it to some other scripting languages e.g. Javascript/Ruby/Perl.

Indeed I am sad that I am not finding any CLI tools (non web-based) for this beyond https://github.com/Anadian/regex-translator . Am hopeful for more suggestions.

Re:How to Use JSON Path by PurpleRamen on Friday May 03, 2024 @09:51AM (#40247670)
I think one popular name, independent of the format, is to call them documents. Or more specifically, the type of database which use them often runs under that term. Maybe specify it as data-document to not confuse it with freeform office-documents.

But one problem is, that each format has slightly different ways it works. Some have nodes which support properties, some not. This makes building a proper query-language a bit more complicated if it should not end up ugly.

Re:How to Use JSON Path by lucianbr on Friday May 03, 2024 @11:14AM (#40248633)
What's the problem with nodes supporting or not some properties? For querying you can treat all nodes as supporting all things, and the user just needs to write a query that acutually works, which is what they would need to do anyway.

Yes, if you want to make the query language reject as incorrect queries that specify a property on a node that can't have properties, it is messy. But is this really necessary? You can write faulty queries anyway. And it's not a programming language for complex systems, where it really helps to prevent mistakes early. SQL works fine with no type safety and so on.

Re:How to Use JSON Path by PurpleRamen on Friday May 03, 2024 @11:43AM (#40248971)
The problem is that you can't easily reuse a query with different formats, with a swallow designed language. You will still end up writing format-targeted queries, which then open the question of why you would even bother in the first place using a watered down language, instead of a format-optimized one.
Re:How to Use JSON Path by jkaptur on Friday May 03, 2024 @12:11PM (#40249302)
Another issue is that the term "document" is also used to refer to XML or HTML.
Re:How to Use JSON Path by PurpleRamen on Friday May 03, 2024 @03:36PM (#40251453)
XML is for data-exchange, competing with JSON and others. So I have no problem putting it to the data-documents, even though it's more a frankenstein. But HTML is an office-document, used for freetext, nobody really should use it for data, even though sometimes it's used that way.
Re:How to Use JSON Path by hgyjnbdet on Saturday May 04, 2024 @01:56AM (#40255241)
XML is also used, literally, for office documents (Word, Excel, etc).
Re:How to Use JSON Path by PurpleRamen on Saturday May 04, 2024 @04:53AM (#40256031)
So are JSON and YAML. The point is whether you have a programmatic and structured handling of a document's content, or whether it's random, where every line and word can demand for a different parser.
Re:How to Use JSON Path by layer8 on Friday May 03, 2024 @11:10AM (#40248573)
Tree-structured data
Re:How to Use JSON Path by irgolic on Friday May 03, 2024 @11:51AM (#40249067)
The general name is semi-structured data, as opposed to structured data (tables) and unstructured data (free text).
Re:How to Use JSON Path by TachyonicBytes on Friday May 03, 2024 @01:35PM (#40250186)
The Readings in Database Systems book calls it "general purpose hierarchical data format" at some point, which seems the most fitting name for me.

You generally don't see yaml or XML be called that, but there's some information on the net that you can find about it.

Re:How to Use JSON Path by emmelaich on Friday May 03, 2024 @01:39PM (#40250218)
Re:How to Use JSON Path by err4nt on Friday May 03, 2024 @04:20PM (#40251947)
Structually, it's a tree. JSON cannot include circularity (like JavaScript objects can) so it's either an object, or nested objects.
Re:How to Use JSON Path by specialist on Friday May 03, 2024 @06:10PM (#40252949)
grove - tree decorated w/ key-value pairs. eg JSON, XML w/o text nodes

object graph - grove plus object references. eg serialization formats

knowledge graph - object graph where parent-child relations are explicit, using subject-verb-object clauses. like how Java Spring "flattens" object graphs.

document - grove plus random text nodes

Re:How to Use JSON Path by zzo38computer on Saturday May 04, 2024 @03:57PM (#40259902)
Although there are similar data structures, not all of them are supersets nor subsets of the data structures of JSON. For example, some data types that JSON lacks are:

- Integers (including 64-bit integers and longer)

- Non-finite floating point (Infinity, NaN)

- Keys of types other than strings

- Non-Unicode strings (e.g. byte sequences, TRON code, etc)

- Date/time

- Links

Additionally, they may differ of whether or not the order of keys should be retained.