English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language

 
 
Thread Tools
Old 23 April 2023, 19:23   #1
_amiga_
Registered User
 
Join Date: Apr 2023
Location: my town
Posts: 4
Parsing JSON in ARexx

Hi!

Given this example JSON object: { "firstName": "John", "lastName": "Doe", "email": "john.doe@example.com", "age": 45, "weight": 67, "admin": true }

how do I parse it in ARexx ?


... and this one (more complex):

{ "name": "Jason Ray", "profession": "Software Engineer", "age": 31, "address": { "city": "New York", "postalCode": 64780, "Country": "USA" }, "languages": ["Java", "Node.js", "JavaScript", "JSON"], "socialProfiles": [ { "name": "Twitter", "link": "https://twitter.com" }, { "name": "Facebook", "link": "https://www.facebook.com" } ] }


Thanks!
_amiga_ is offline  
Old 23 April 2023, 20:30   #2
daxb
Registered User
 
Join Date: Oct 2009
Location: Germany
Posts: 3,303
Maybe this is helpful: https://wiki.amigaos.net/wiki/AmigaO..._ARexx_Parsing
daxb is offline  
Old 23 April 2023, 22:15   #3
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,118
This is an interesting one because IIRC Rexx uses a variable structure not unlike JS.
Karlos is online now  
Old 24 April 2023, 15:19   #4
daxb
Registered User
 
Join Date: Oct 2009
Location: Germany
Posts: 3,303
Parsing by pattern:
Code:
/**/

object='"firstName": "John", "lastName": "Doe", "email": "john.doe@example.com", "age": 45, "weight": 67, "admin": true'

PARSE VAR object '"firstName":' fN ',' '"lastName":' lN ',' '"email":' email ',' '"age":' age ',' '"weight":' weight ',' 'admin":' admin

SAY fN lN email age weight admin
Maybe there is another/better way? The complex example need other techniques.
daxb is offline  
Old 25 April 2023, 10:28   #5
_amiga_
Registered User
 
Join Date: Apr 2023
Location: my town
Posts: 4
I've found this Rexx code for parsing JSON.
It's for os/2, can somebody adapt it to work in ARexx ?


/*
* Filename: JSon2Rx.cmd
* Author: Jan-Erik
* Created: Sat Feb 23 2013
* Purpose: Interpret a JSon-file and place in stem
* Changes:
*/

IF ARG() > 0 THEN
IF STREAM( ARG(1), 'C', 'QUERY EXISTS' ) <> '' THEN
f_name = ARG(1)
ELSE
f_name = 'london.json'
ELSE
f_name = 'london.json'

IF STREAM( f_name, 'C', 'QUERY EXISTS' ) = '' THEN RETURN 1
f_size = STREAM( f_name, 'C', 'QUERY SIZE' )
f_input = CHARIN( f_name, 1, f_size )
CALL STREAM f_name, 'C', 'CLOSE'

stem = 'json.'
DO WHILE fromJSon( f_input ) > 0
CALL CHAROUT 'STDERR', 'Parts of the file missing! Press Enter to read it again. '
PARSE PULL
f_size = STREAM( f_name, 'C', 'QUERY SIZE' )
f_input = CHARIN( f_name, 1, f_size )
CALL STREAM f_name, 'C', 'CLOSE'
END

DO i = 1 TO json.0
SAY json.i '=' VALUE( json.i )
END

RETURN 0

fromJSon: PROCEDURE EXPOSE (stem)
!_str_json_! = ARG(1)
!_stem_counter_! = 0
!_not_in_key_range_! = SPACE( TRANSLATE( XRANGE(),, XRANGE( 'a', 'z' )||XRANGE( 'A', 'Z' )||XRANGE( 0, 9 )||'_' ), 0 )
IF SYMBOL( stem ) <> 'LIT' THEN
stem = 'STEM'
stem = STRIP( TRANSLATE( stem ), 'T', '.' )
!_s_name_! = stem
DO WHILE LENGTH( !_str_json_! ) > 0
PARSE VALUE !_str_json_! WITH !_key_!':'!_str_json_!
IF POS( '{', STRIP( !_key_! ) ) = 1 THEN
DO
PARSE VALUE !_key_! WITH '{'!_key_!
!_key_! = SPACE( TRANSLATE( !_key_!,, !_not_in_key_range_! ), 0 )
END
CALL calc4Pos
IF !_strt_brkt_! < !_key_val_del_! & !_strt_brkt_! < !_end_brkt_! & 0 < !_strt_brkt_! THEN
DO
PARSE VALUE !_str_json_! WITH '{'!_str_json_!
!_key_! = SPACE( TRANSLATE( !_key_!,, !_not_in_key_range_! ), 0 )
IF !_strt_sqr_brkt_! < !_strt_brkt_! & 0 < !_strt_sqr_brkt_! THEN
DO
!_s_name_! = !_s_name_!'.'TRANSLATE( !_key_! )'.0'
IF POS( '.', !_s_name_! ) > 0 THEN
DO
PARSE VALUE REVERSE( !_s_name_! ) WITH !_n_!'.'!_s_name_!
!_n_! = REVERSE( !_n_! )
!_s_name_! = REVERSE( !_s_name_! )
IF DATATYPE( !_n_!, 'W' ) THEN
DO
!_n_! = !_n_! + 1
CALL VALUE !_s_name_!'.0', !_n_!
END
!_s_name_! = !_s_name_!'.'!_n_!
END
ELSE
!_s_name_! = !_s_name_!'.'STRIP( TRANSLATE( !_key_! ),, '"' )
END
ELSE
!_s_name_! = !_s_name_!'.'STRIP( TRANSLATE( !_key_! ),, '"' )
ITERATE
END
!_stem_counter_! = !_stem_counter_! + 1
PARSE VALUE !_str_json_! WITH !_val_!','!_str_json_!
PARSE VALUE !_val_! WITH !_val_!'}'!_rest_!
!_key_! = SPACE( TRANSLATE( !_key_!,, !_not_in_key_range_! ), 0 )
IF POS( '"', !_val_! ) > 0 THEN
PARSE VALUE !_val_! WITH '"'!_val_!'"'
!_val_! = STRIP( STRIP( STRIP( STRIP( STRIP( STRIP( !_val_! ), 'T', D2C(10) ), 'T', D2C(13) ) ),, '"' ) )
CALL VALUE !_s_name_!'.'TRANSLATE( !_key_! ), !_val_!
CALL VALUE VALUE( stem )'.'!_stem_counter_!, !_s_name_!'.'TRANSLATE( !_key_! )
CALL VALUE VALUE( stem )'.0', !_stem_counter_!
IF ( !_end_brkt_! < !_key_val_sep_! | !_key_val_sep_! = 0 ) & ( !_end_brkt_! < !_end_sqr_brkt_! | !_end_sqr_brkt_! = 0 ) THEN
DO
!_n_! = ''
!_rest_! = '}'||SPACE( TRANSLATE( !_rest_!,, SPACE( TRANSLATE( XRANGE(),, '}]' ), 0 ) ), 0 )
DO WHILE LENGTH( !_rest_! ) > 0
PARSE VALUE !_rest_! WITH !_sign_! +1 !_rest_!
IF POS( '.', !_s_name_! ) > 0 THEN
DO
PARSE VALUE REVERSE( !_s_name_! ) WITH !_n_!'.'!_s_name_!
!_s_name_! = REVERSE( !_s_name_! )
!_n_! = REVERSE( !_n_! )
IF DATATYPE( !_n_!, 'W' ) & LEFT( !_rest_!, 1 ) <> ']' THEN
DO
!_n_! = !_n_! + 1
!_s_name_! = !_s_name_!'.'!_n_!
END
END
END
END
END
RETURN COUNTSTR( '.', !_s_name_! ) /* IF retval > 0 THEN INCOMPLETE FILE OR ERROR IN FUNCTION */

calc4Pos:
!_strt_sqr_brkt_! = POS( '[', !_str_json_! )
!_end_sqr_brkt_! = POS( ']', !_str_json_! )
!_strt_brkt_! = POS( '{', !_str_json_! )
!_end_brkt_! = POS( '}', !_str_json_! )
!_key_val_sep_! = POS( ',', !_str_json_! )
!_key_val_del_! = POS( ':', !_str_json_! )
RETURN 0


/* Count the number of occurances of needle in haystack from start pos to end pos (whole string by default) */
COUNTSTR: PROCEDURE /* needle, haystack< <, startpos>, endpos> */
IF TRACE() = '?I' THEN CALL TRACE 'O'
IF ARG() < 2 THEN RETURN -1
IF DATATYPE( ARG(3), 'W' ) THEN
next = ARG(3)
ELSE
next = 1
needle = ARG(1)
haystack = ARG(2)
IF DATATYPE( ARG(4), 'W' ) THEN
haystack = SUBSTR( haystack, next, ABS( ARG(4) - next ) )
next = 1
count = 0
DO WHILE next > 0
next = POS( needle, haystack, next )
IF next > 0 THEN DO
next = next + LENGTH( needle )
count = count + 1
END
END
RETURN count
_amiga_ is offline  
Old 29 April 2023, 02:49   #6
tygre
Returning fan!
 
tygre's Avatar
 
Join Date: Jan 2011
Location: Montréal, QC, Canada
Posts: 1,434
Hi _amiga_!

This Rexx code comes from here?

What happens if you try to execute it?

Cheers!
tygre is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Stream parsing techniques colourspace Coders. Asm / Hardware 5 07 May 2021 19:29
Parsing an HTML file in Arexx Gundam Coders. General 7 09 April 2014 00:16
xml parsing in arexx amiga_user Coders. General 2 17 November 2011 15:42
error parsing global configuration file line 16 DDNI project.WHDLoad 7 21 March 2011 13:09
arexx help jimbobrocks92 Coders. General 4 19 January 2011 12:50

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 On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 09:55.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.08733 seconds with 13 queries