ref: 0ada4ab3daa58d556df43b1f4ad5c248415fbf8d
parent: 007e345af521d4d119ea3759ae0b8bec369609cb
author: Jean-André Santoni <jean.andre.santoni@gmail.com>
date: Thu Aug 21 11:19:24 EDT 2025
Add verbs
--- a/jget.c
+++ b/jget.c
@@ -3,10 +3,20 @@
#include <bio.h>
#include <json.h>
+void usage(void)
+{+ print("jget key foo");+ print("jget idx 0");+ print("jget str foo");+}
+
void
main(int argc, char **argv)
{- USED(argc, argv);
+ if(argc != 3){+ usage();
+ exits(nil);
+ }
Biobuf bin;
char *s;
@@ -20,20 +30,34 @@
j = jsonparse(s);
if(j == nil)
- sysfatal("jsonparse failed");+ sysfatal("jsonparse failed");- JSON *output = jsonbyname(j, "output");
- if(output == nil)
- sysfatal("no output");+ JSONfmtinstall();
- if(output->t == JSONArray) {- JSONEl *first = output->first;
- JSON *cont = jsonbyname(first->val, "content");
- if(cont && cont->t == JSONArray) {- JSONEl *first = cont->first;
- JSON *text = jsonbyname(first->val, "text");
- print("%s\n", jsonstr(text));- }
+ char *verb = argv[1];
+ char *obj = argv[2];
+
+ if(!strcmp(verb, "key")){+ JSON *out = jsonbyname(j, obj);
+ if(out == nil)
+ sysfatal("could not find key");+
+ print("%J\n", out);+ }else if(!strcmp(verb, "str")){+ JSON *out = jsonbyname(j, obj);
+ if(out == nil)
+ sysfatal("could not find key");+
+ print("%s\n", jsonstr(out));+ }else if(!strcmp(verb, "idx")){+ if(j->t != JSONArray)
+ sysfatal("only array can be indexed");+ int ind = atoi(obj);
+ JSONEl *el = j->first;
+ for(int i=0;i<ind;i++)
+ el = el->next;
+
+ print("%J\n", el->val);}
free(s);
--
⑨