How to...extract a string from a .json-file?

I have a small .json-file:
{"created_at":"2019-09-06T15:51:10Z","id":19814066,"tag":"v0.9b11"}
Now I want to get the value for tag - what's the best to do that? My only idea was TPIPE, but after RFTMing I have the impression that this can only be used to filter line (e.g. /string always returns "lines"). I thought regex might be nice - but I don't see how I can get the matches of a regex-search...
 

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
One option would be to use @word and use comma as the separator. Then the 3rd word will contain the "tag":"value" string. Then use @word again on that and pick out the 2nd word.

A second option is to use @regexsub.
Code:
$ echo %@regexsub[1,".*,.*,.*:\"(.*)\"",{"created_at":"2019-09-06T15:51:10Z","id":19814066,"tag":"v0.9b11"}]
v0.9b11
You can use @line to extract a specific line from the file.
 
May 20, 2008
11,411
99
Syracuse, NY, USA
A crude first attempt:
Code:
v:\> type json.json
{"created_at":"2019-09-06T15:51:10Z","id":19814066,"tag":"v0.9b11"}

v:\> tpipe /input=json.json /grep=3,0,0,0,0,0,0,0,"19814066" /replace=4,1,0,1,0,0,0,0,0,"^.*tag.:(.*).$","$1"
"v0.9b11"

The grep isn't necessary in this simple example, but I figured you'd be looking for the tag of a particular id. "$1" is what was matched inside ().
 
May 20, 2008
11,411
99
Syracuse, NY, USA
This gets rid of the quotes.
Code:
v:\> type json.json
{"created_at":"2019-09-06T15:51:10Z","id":19814066,"tag":"v0.9b11"}

v:\> tpipe /input=json.json /grep=3,0,0,0,0,0,0,0,"19814066" /replace=4,1,0,1,0,0,0,0,0,"^.*tag.:\"(.*)\".*$","$1"
v0.9b11
 
May 20, 2008
11,411
99
Syracuse, NY, USA
Here's another approach:
Code:
v:\> type json.json
{"created_at":"2019-09-06T15:51:10Z","id":19814066,"tag":"v0.9b11"}

v:\> echo %@word["^"",9,%@execstr[type json.json | tpipe /grep=3,0,0,0,0,0,0,0,19814066]]
v0.9b11

On Windows 10 with WSL,
Code:
v:\> echo %@word["^"",9,%@execstr[type json.json | wsl grep 19814066]]
v0.9b11
 
May 20, 2008
11,411
99
Syracuse, NY, USA
And going hog-wild with WSL,
Code:
v:\> wsl grep 19814066 %@wslpath[%@truename[json.json]] | wsl cut -d "\"" -f10
v0.9b11
 
Jan 19, 2011
604
14
Norman, OK
Code:
0:00:00.080
[D:\temp]
14:26:03 $ type json.txt
{"created_at":"2019-09-06T15:51:10Z","id":19814066,"tag":"v0.9b11"}

0:00:00.003
[D:\temp]
14:26:07 $ jq ".tag" < json.txt
"v0.9b11"

Get "jq" here.
 
  • Like
Reactions: Joe Caverly
Code:
0:00:00.080
[D:\temp]
14:26:03 $ type json.txt
{"created_at":"2019-09-06T15:51:10Z","id":19814066,"tag":"v0.9b11"}

0:00:00.003
[D:\temp]
14:26:07 $ jq ".tag" < json.txt
"v0.9b11"

Get "jq" here.

Never knew about this command for working with .json files. Afer a quick
sudo apt-get install jq
via wsl, I can now do
Code:
e:\utils>echo {"created_at":"2019-09-06T15:51:10Z","id":19814066,"tag":"v0.9b11"} | wsl jq ".tag"
"v0.9b11"
on the OPs example. Added to my notebook for future reference. Thanks!
 
Powershell also works well with json;
Code:
pshell /s "$Json = '{"created_at":"2019-09-06T15:51:10Z","id":19814066,"tag":"v0.9b11"}'"
pshell /s "$Json"
pshell /s "$ObjJson = ConvertFrom-Json -InputObject $Json"
pshell /s "$objJson.created_at"
pshell /s "$ObjJson.id"
pshell /s "$ObjJson.tag"

...which returns;
Code:
e:\utils>pshell /s "$Json = '{"created_at":"2019-09-06T15:51:10Z","id":19814066,"tag":"v0.9b11"}'"

e:\utils>pshell /s "$Json"
{"created_at":"2019-09-06T15:51:10Z","id":19814066,"tag":"v0.9b11"}

e:\utils>pshell /s "$ObjJson = ConvertFrom-Json -InputObject $Json"

e:\utils>pshell /s "$objJson.created_at"
2019-09-06T15:51:10Z

e:\utils>pshell /s "$ObjJson.id"
19814066

e:\utils>pshell /s "$ObjJson.tag"
v0.9b11

Joe
 
Aug 23, 2010
637
9
Never knew? I googled it the first time I asked the topic question.
Also,
Code:
jq -r .tag

P.S.
jq also comes with Cygwin.
 
Similar threads
Thread starter Title Forum Replies Date
A Installer /extract: extracts into current directory if target not exists Support 12
vefatica Extract fields with TPIPE? Support 10
D /extract: option for the installer Support 4
C tpipe and extract email addresses Support 1
Phileosophos Unzip doesn't extract to specified output folder Support 5
P Inserting string into a variable. Support 3
M Backquoted parameters used in GoSub don't pass string as a single parameter Support 2
H Behavior change re piping a string to a Java program Support 1
C email[string] function Support 10
D Finding a string position in a larger string Support 2
D Faster string search Support 8
D btm file command line augments comparing for string or math. Support 12
B How can TCC make an output as a part of command string Support 2
vefatica DO /L, where did my string go? Support 6
Alpengreis A leading withespace in Hash-String is not included? Support 1
T TPipe: Replacing String with Double Quotes Support 3
J WAD utf8decode string only results in ? chars Support 2
scooter_de How to? How to split the a string? Support 4
C custom string compare? Support 8
C Best way to validate a string is a YYYYMMDD Support 7
Phileosophos Documentation TPIPE /string type code confusion Support 4
vefatica WAD "tokens=*" and literal string Support 4
vefatica Back-refs in TPIPE's /replace's replacement string? Support 2
vefatica @B64ENCODE[s,string]? Support 8
vefatica Random access to the characters in a string? Support 4
vefatica @WORD[], quoted string? Support 8
H "Here-String" redirection not working Support 6
C Can't remove leading quote from a string Support 2

Similar threads