From: Tim Landscheidt <tim@tim-landscheidt.de>
To: help-gnu-emacs@gnu.org
Subject: Re: Parse a field in JSON given a path to the field
Date: Wed, 26 Jan 2022 22:05:49 +0000 [thread overview]
Message-ID: <8735lagnk2.fsf@vagabond.tim-landscheidt.de> (raw)
In-Reply-To: 87r1bmpm8n.fsf@alshehhi.io
Husain Alshehhi <husain@alshehhi.io> 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>
Tim
next prev parent reply other threads:[~2022-01-26 22:05 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-11 18:42 Parse a field in JSON given a path to the field Husain Alshehhi
2021-11-13 13:15 ` Andreas Röhler
2021-11-13 14:08 ` Yuri Khan
2022-01-26 22:05 ` Tim Landscheidt [this message]
2022-01-26 22:19 ` 2QdxY4RzWzUUiLuE
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8735lagnk2.fsf@vagabond.tim-landscheidt.de \
--to=tim@tim-landscheidt.de \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).