LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-07-2020, 08:27 PM   #1
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Parse query uri to from json


I have a query uri string like this

Code:
module=API&method=ImageGraph.get&idSite=3&apiModule=VisitsSummary&apiAction=get&token_auth=anonymous
I want to convert it to json, and then convert it back to a uri string.
I'm trying to use jq to do this, I think it can do this, but I'm not sure now.

Code:
{
  "module": "API",
  "method": "ImageGraph.get",
  "idSite": "3",
  "apiModule": "VisitsSummary",
  "apiAction": "get",
  "token_auth": "anonymous"
}
I feel like I'm close with converting json to a uri, but I'm pretty lost. The output does not match the above uri.

Code:
echo '{"module":"API","method":"ImageGraph.get","idSite":"3","apiModule":"VisitsSummary","apiAction":"get","token_auth":"anonymous"}' \
  | jq '@uri "\(.)"'
"%7B%22module%22%3A%22API%22%2C%22method%22%3A%22ImageGraph.get%22%2C%22idSite%22%3A%223%22%2C%22apiModule%22%3A%22VisitsSummary%22%2C%22apiAction%22%3A%22get%22%2C%22token_auth%22%3A%22anonymous%22%7D"
Some resources I've been using..
 
Old 01-08-2020, 01:52 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I think better to solve it with perl/python or probably awk.
Code:
A='module=API&method=ImageGraph.get&idSite=3&apiModule=VisitsSummary&apiAction=get&token_auth=anonymous'
IFS=' ' read -r -a AA <<< "${A//&/ }"
printf "%s\n" "${AA[@]}" | sed 's/^\([^=]*\)=\([^=]*\)$/"\1": "\2",/'
will split the string.
 
Old 01-08-2020, 06:14 AM   #3
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
The reason the output does not match is because jq's @uri function converts any reserved URI characters to their percent-encoded form. So while it constructs a URI given JSON data, it also (properly) encodes them. It you want to output a non-encoded URI, something like this will work.
Code:
jq -r 'to_entries | map("\(.key)=\(.value)") | join("&")'
It splits all key/value pairs into their own hash, which can then be "mapped" over, collecting each key/value and using a format string "key=value." Finally, join the resulting strings with an '&' character.

As an aside, I'd recommend really studying the jq manual--you'll discover all sorts of neat things you can do.

Last edited by individual; 01-08-2020 at 06:16 AM.
 
1 members found this post helpful.
Old 01-08-2020, 06:23 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by individual View Post
As an aside, I'd recommend really studying the jq manual--you'll discover all sorts of neat things you can do.
Indeed.
Every time I see some-one offer such an exemplary answer, I think I should go out and find a reason to learn jq properly.
 
Old 01-08-2020, 09:20 AM   #5
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Original Poster
Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Quote:
I think better to solve it with perl/python or probably awk.
Python can yes. After some searching, I can write a small program to encode / decode (I put a example below). I was hoping to glean some info too though from jq that the manual was being confusing about.

Code:
import urllib.parse
# Encode
urllib.parse.quote_plus(string)
# Decode
urllib.parse.unquote(url)
Quote:
As an aside, I'd recommend really studying the jq manual--you'll discover all sorts of neat things you can do.
Yes, I referenced the manual but I suppose I got caught up on the @uri part.

the to_entries and -r --raw-input are both very helpful

I see to_entries does the following, which I assume lets it do its thing. I got confused since simply inputting it works, and shows it formatted pretty.
Code:
{ "module": "API" } ->
[
  {
    "key": "module",
    "value": "API"
  }
]
jq seems to be like awk where it has its own sublanguage

EDIT:
Is it possible for jq to parse a query uri?

Last edited by Sefyir; 01-08-2020 at 10:10 AM.
 
Old 01-08-2020, 10:20 AM   #6
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Sefyir View Post
jq seems to be like awk where it has its own sublanguage
In a way, yes, but it is a domain specific language (DSL) because it primarily operates on JSON data (though it is possible to pass non-JSON data), whereas awk works with any text.
Quote:
EDIT:
Is it possible for jq to parse a query uri?
Could you elaborate?
EDIT: Assuming you're asking if jq can take a formatted URI and turn it into an object, then yes.
Code:
echo "module=API&method=ImageGraph.get&idSite=3&apiModule=VisitsSummary&apiAction=get&token_auth=anonymous" | jq -rR 'split("&") | map(split("=")) | reduce .[] as $a ({}; . + {"\($a.[0])": $a.[1]})'
Result:
Code:
{
  "module": "API",
  "method": "ImageGraph.get",
  "idSite": "3",
  "apiModule": "VisitsSummary",
  "apiAction": "get",
  "token_auth": "anonymous"
}
I know there is a lot going on there, but see if you can figure out how it all works by using the manual as a reference.

Last edited by individual; 01-08-2020 at 10:23 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Elasticsearch Query Python Requests Json Parse Exception snowman81 Programming 1 11-25-2017 09:30 PM
LXer: How to parse JSON string via command line on Linux LXer Syndicated Linux News 0 09-05-2013 06:32 PM
Parse JSON using Linux command Stefanus Linux - Newbie 2 11-19-2012 10:25 PM
E:Malformed line 64 in source list /etc/apt/sources.list (URI parse) Help Please PacerguyDon Linux - Software 2 11-14-2012 11:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration