Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

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

Feb
61
1
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...
 
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.
 
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 ().
 
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
 
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
 
And going hog-wild with WSL,
Code:
v:\> wsl grep 19814066 %@wslpath[%@truename[json.json]] | wsl cut -d "\"" -f10
v0.9b11
 
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.
 
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
 
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

Back
Top