Help with JSONPARSE

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
andywilliams
Newbie
Posts: 4
Joined: Sat Apr 21, 2007 10:00 am

Help with JSONPARSE

Post by andywilliams » Tue May 31, 2022 3:11 pm

Hello,

Would anyone be able to offer some assistance to me parsing some JSON. I have been trying to do this for about a week now an can't seem to make any progress.

The JSON I am interested is the response from this URL:

https://1ml.com/channel?json=true

I would like to be able to parse out the following:
"channel_ID"
"fee_base_msat"
"fee_rate_milli_msat"

Any help with this would be most gratefully appreciated.

Regards

AW

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1106
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Help with JSONPARSE

Post by Dorian (MJT support) » Tue May 31, 2022 4:49 pm

Here you go. This thread explains the reason for the problem.

The solution was to fix it with StringReplace, and then parse.

Code: Select all

//https://1ml.com/channel?json=true
//"channel_id"
//"fee_base_msat"
//"fee_rate_milli_msat"

HTTPRequest>https://1ml.com/channel?json=true,,GET,,sJSON
stringreplace>sJSON,[,{"things":[,sJSON
stringreplace>sJSON,],]},sJSON


JSONParse>sJSON,$.things[*].channel_id,chanID
JSONParse>sJSON,$.things[*].node1_policy.fee_base_msat,feebase
JSONParse>sJSON,$.things[*].node1_policy.fee_rate_milli_msat,feerate

mdl>%chanID_count% results


//let's loop through the output 
Let>k=0
Repeat>k
  Let>k=k+1
  Let>this_chanID=chanID_%k%
  MessageModal>this_chanID

  Let>this_feebase=feebase_%k%
  MessageModal>this_feebase

  Let>this_feerate=feerate_%k%
  MessageModal>this_feerate

Until>k=chanID_count


Yes, we have a Custom Scripting Service. Message me or go here

hagchr
Automation Wizard
Posts: 314
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Help with JSONPARSE

Post by hagchr » Tue May 31, 2022 5:12 pm

alternative:

Code: Select all

HTTPRequest>https://1ml.com/channel?json=true,,GET,,sJSON

JSONParse>sJSON,$..channel_id,ChannelID
JSONParse>sJSON,$..fee_base_msat,feebase
JSONParse>sJSON,$..fee_rate_milli_msat,feerate
I note you have more than one feebase/feerate per channel so depends how you want to parse it out.

Eg feerate for node1_policy:

Code: Select all

JSONParse>sJSON,$..node1_policy.fee_rate_milli_msat,feerate

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1106
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Help with JSONPARSE

Post by Dorian (MJT support) » Wed Jun 01, 2022 8:12 am

hagchr wrote:
Tue May 31, 2022 5:12 pm
alternative:

Code: Select all

HTTPRequest>https://1ml.com/channel?json=true,,GET,,sJSON

JSONParse>sJSON,$..channel_id,ChannelID
JSONParse>sJSON,$..fee_base_msat,feebase
JSONParse>sJSON,$..fee_rate_milli_msat,feerate
I note you have more than one feebase/feerate per channel so depends how you want to parse it out.

Eg feerate for node1_policy:

Code: Select all

JSONParse>sJSON,$..node1_policy.fee_rate_milli_msat,feerate
Just wanted to say thank you. :)
Yes, we have a Custom Scripting Service. Message me or go here

andywilliams
Newbie
Posts: 4
Joined: Sat Apr 21, 2007 10:00 am

Re: Help with JSONPARSE

Post by andywilliams » Sat Jun 04, 2022 1:14 pm

Thanks both! I think I prefer hagchr's method but both work. I realized I was looking at an anonymous array but was unsure of the syntax required to parse the array elements.

I am now able to do what I wanted.

Thanks again.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts