From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: 2QdxY4RzWzUUiLuE@potatochowder.com Newsgroups: gmane.emacs.help Subject: Re: Parse a field in JSON given a path to the field Date: Wed, 26 Jan 2022 16:19:36 -0600 Message-ID: References: <87r1bmpm8n.fsf@alshehhi.io> <8735lagnk2.fsf@vagabond.tim-landscheidt.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10773"; mail-complaints-to="usenet@ciao.gmane.io" To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jan 26 23:21:21 2022 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nCqfJ-0002bD-8i for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 26 Jan 2022 23:21:21 +0100 Original-Received: from localhost ([::1]:35614 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nCqfH-0005d4-Mm for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 26 Jan 2022 17:21:19 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:53806) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <2QdxY4RzWzUUiLuE@potatochowder.com>) id 1nCqdn-0005cj-8f for help-gnu-emacs@gnu.org; Wed, 26 Jan 2022 17:19:47 -0500 Original-Received: from www458.your-server.de ([136.243.165.62]:48882) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <2QdxY4RzWzUUiLuE@potatochowder.com>) id 1nCqdl-0006gp-E3 for help-gnu-emacs@gnu.org; Wed, 26 Jan 2022 17:19:46 -0500 Original-Received: from sslproxy06.your-server.de ([78.46.172.3]) by www458.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92.3) (envelope-from <2QdxY4RzWzUUiLuE@potatochowder.com>) id 1nCqde-0009zw-Ug for help-gnu-emacs@gnu.org; Wed, 26 Jan 2022 23:19:39 +0100 Original-Received: from [2607:fb90:42ff:ceda:4da7:111b:9805:e8] (helo=localhost) by sslproxy06.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from <2QdxY4RzWzUUiLuE@potatochowder.com>) id 1nCqde-000VHj-9U for help-gnu-emacs@gnu.org; Wed, 26 Jan 2022 23:19:38 +0100 Mail-Followup-To: help-gnu-emacs@gnu.org Content-Disposition: inline In-Reply-To: <8735lagnk2.fsf@vagabond.tim-landscheidt.de> X-Authenticated-Sender: 2QdxY4RzWzUUiLuE@potatochowder.com X-Virus-Scanned: Clear (ClamAV 0.103.5/26434/Wed Jan 26 10:25:33 2022) Received-SPF: pass client-ip=136.243.165.62; envelope-from=2QdxY4RzWzUUiLuE@potatochowder.com; helo=www458.your-server.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:135543 Archived-At: On 2022-01-26 at 22:05:49 +0000, Tim Landscheidt wrote: > Husain Alshehhi wrote: > > > Does emacs provide a function that can return a path from a JSON object? I find something like this useful if I want a field from a nested, large JSON. Here is an example of a JSON string > > > ,---- > > | { > > | "field" : { > > | "field1" : { > > | "field2" : { > > | "field3" : "value" > > | } > > | } > > | } > > `---- > > > I do something like: > > > ,---- > > | ;; assume that the value is in json-string > > | (let ((json (json-parse json-string)) > > | (field (gethash "field" json)) > > | (field1 (gethash "field1" field)) > > | (field2 (gethash "field2" field1)) > > | (field3 (gethash "field3" field2))) > > | ;; process field3 > > | ) > > `---- > > > I wonder if there is something like > > > ,---- > > | (let ((field3 (json-parse-path "field/field1/field2/field3" json-string))) > > | ;; process field3 > > | ) > > `---- > > If the path is fixed, you could also use let-alist: > > | ELISP> (let ((json (json-parse-string "{\n \"field\": {\n \"field1\": {\n \"field2\": {\n \"field3\": \"value\"\n }\n }\n }\n}\n" > | :object-type 'alist))) > | (let-alist json > | (message "field/field1/field2/field3 = %S" .field.field1.field2.field3))) > | "field/field1/field2/field3 = \"value\"" > | ELISP> I wrote this in Common Lisp; it should work (but I haven't tested it) in Elisp: (defun json-drill (json keys) (let ((value json)) (dolist (key keys value) (setf value (gethash key value))))) The keys argument is a list of keys rather than a single string with the keys separated by slashes. Your example would look like this: (let ((json-data (json-parse json-string))) (let ((field3 (json-drill json '("field" "field1" "field2" "field3")))) ;; process field3 )) Obviously, whatever you pass to json-drill as keys doesn't have to be a constant.