konrad1977 · 2022-04-04 19:46
Hi!
Im trying to parse some json and after I read it I get something like this:
How do I access the value of "\_\_CODE\_SIGNING\_ALLOWED\_appletvos"?
`[((action . "build") (buildSettings (__CODE_SIGNING_ALLOWED_appletvos . "NO") (__CODE_SIGNING_ALLOWED_iphoneos . "NO") (__CODE_SIGNING_ALLOWED_watchos . "NO") (_WRAPPER_CONTENTS_DIR_SHALLOW_BUNDLE_NO . "/Contents") (_WRAPPER_PARENT_PATH_SHALLOW_BUNDLE_NO . "/..") (_WRAPPER_RESOURCES_DIR_SHALLOW_BUNDLE_NO . "/Resources") (ACTION . "build") (AD_HOC_CODE_SIGNING_ALLOWED . "NO") (ALLOW_TARGET_PLATFORM_SPECIALIZATION . "NO") (ALTERNATE_GROUP . "staff") (ALTERNATE_MODE . "u+w,go-w,a+rX") ...) (target . "MyTarget"))]`
I am using json.el
Is there a good tutorial on using elisp and json?
Sorry for the bad formatting.
ieure · 2022-04-04 20:08
Assuming your value is let-bound to `json-parsed-value`:
(thread-last (elt json-parsed-value 0) cadr (assoc 'buildSettings) cdr (assoc '__CODE_SIGNING_ALLOWED_appletvos) cdr)
Note that it looks like your value is an array. This will work if it only has a single value, but if there's a case with multiple, you'll need to decide which one is the one you need somehow.
> Is there a good tutorial on using elisp and json?
No, there's no need. JSON is deserialized into Lisp data structures, so the normal Lisp operations to access them are the only thing required. See [Lists](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lists.html) and [Sequences, Arrays, and Vectors](https://www.gnu.org/software/emacs/manual/html_node/elisp/Sequences-Arrays-Vectors.html) in the Emacs Lisp manual.
xenodium · 2022-04-05 03:05
I had some warnings I needed to filter out, so you can likely do without by `string-join` and `seq-filter`:
(let ((json (json-read-from-string
(string-join
(seq-filter
(lambda (item)
;; Remove lines from warnings like:
;; objc[32355]: Class AMSupportURLConnectionDelegate is implemented in both /usr/lib/libamsupport.dylib (0x1e0d2f130) and /Library/Apple/System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/MobileDevice (0x1040082c8). One of the two will be used. Which one is undefined.
(not (string-prefix-p "objc" item)))
(process-lines "xcodebuild" "-showBuildSettings" "-configuration" "Debug" "-json"))
"\n"))))
(let-alist (seq-elt json 0)
(message "--> %s" .buildSettings.BUILD_DIR)))
\--> /Users/someone/Library/Developer/Xcode/DerivedData/Foo-cgindakuoopxrrgqkiippmldvmpk/Build/Products
konrad1977 · 2022-04-05 05:49
Thanks. Ended up with this which works.
(defun swift-additions:get-bundle-identifier (config)
"Get bundle identifier (as CONFIG)."
(let* ((default-directory (projectile-project-root))
(json (call-process-to-json "xcrun" "xcodebuild" "-showBuildSettings" "-configuration" config "-json")))
(let-alist (seq-elt json 0) .buildSettings.PRODUCT_BUNDLE_IDENTIFIER)))
shaggy_amreeki · 2022-04-06 15:09
Is call-process-to-json an inbuilt function? I couldn't find it.
konrad1977 · 2022-04-06 17:14
No its not:
You will find it here. I've taken it from swift-mode (swift-mode-repl.el). I will add a license note about that soon.
[https://github.com/konrad1977/emacs/blob/main/localpackages/swift-additions.el](https://github.com/konrad1977/emacs/blob/main/localpackages/swift-additions.el)
shaggy_amreeki · 2022-04-06 19:08
Ah cool function. Might come in handy, thanks.