@@ -6213,12 +6213,16 @@ end subroutine json_get_by_path_rfc6901
6213
6213
! type(json_value),pointer :: dat,p
6214
6214
! logical :: found
6215
6215
! !...
6216
+ ! call json%initialize(path_mode=3)
6217
+
6216
6218
! call json%get(dat,"$['store']['book'][1]['title']",p,found)
6217
6219
! ````
6218
6220
!
6219
6221
! The first character `$` is optional, and signifies the root
6220
- ! of the structure. If it is not present, the the first key is
6221
- ! taken to be in the `me` object.
6222
+ ! of the structure. If it is not present, then the first key
6223
+ ! is taken to be in the `me` object.
6224
+ !
6225
+ ! Single or double quotes may be used
6222
6226
!
6223
6227
! ### See also
6224
6228
! * [[json_get_by_path_default]] - subset of JSONPath "dot-notation"
@@ -6230,7 +6234,12 @@ end subroutine json_get_by_path_rfc6901
6230
6234
! @note Uses 1-based array indices (same as [[json_get_by_path_default]],
6231
6235
! but unlike [[json_get_by_path_rfc6901]] which uses 0-based indices).
6232
6236
!
6233
- ! @warning The `create` logic hasn't been added yet !!!
6237
+ ! @warning Note that if using single quotes, this routine cannot parse
6238
+ ! a key containing `']`. If using double quotes, this routine
6239
+ ! cannot parse a key containing `"]`. If the key contains both
6240
+ ! `']` and `"]`, there is no way to parse it using this routine.
6241
+ !
6242
+ ! @warning The `create` logic hasn't been added yet !
6234
6243
6235
6244
subroutine json_get_by_path_jsonpath_bracket (json ,me ,path ,p ,found ,create_it ,was_created )
6236
6245
@@ -6267,6 +6276,9 @@ subroutine json_get_by_path_jsonpath_bracket(json,me,path,p,found,create_it,was_
6267
6276
! ! traversing the structure
6268
6277
integer (IK) :: i ! ! counter
6269
6278
integer (IK) :: ilen ! ! length of `path` string
6279
+ logical (LK) :: double_quotes ! ! if the keys are enclosed in `"`,
6280
+ ! ! rather than `'` tokens.
6281
+
6270
6282
logical (LK) :: create ! ! if the object is to be created
6271
6283
logical (LK) :: created ! ! if `create` is true, then this will be
6272
6284
! ! true if the leaf object had to be created
@@ -6335,26 +6347,35 @@ subroutine json_get_by_path_jsonpath_bracket(json,me,path,p,found,create_it,was_
6335
6347
6336
6348
! get the next token by checking:
6337
6349
!
6338
- ! * is the token after istart a quote?
6339
- ! if so, then search for the next `']`
6340
- ! ['']
6341
- !
6342
- ! * if not, then maybe it is a number,
6343
- ! so search for the next `]`
6344
- ! [1]
6350
+ ! * [''] -- is the token after istart a quote?
6351
+ ! if so, then search for the next `']`
6345
6352
!
6346
- ! istart iend
6347
- ! | |
6348
- ! [abcdefg][h][ijk]
6353
+ ! * [1] -- if not, then maybe it is a number,
6354
+ ! so search for the next `]`
6349
6355
6350
6356
! verify length of remaining string
6351
6357
if (istart+2 <= ilen) then
6352
- if (path(istart+1 :istart+1 ) == single_quote) then ! ['
6353
- istart = istart + 1 ! move to ' index
6354
- ! it should be a key value
6355
- iend = istart + index (path(istart+1 :ilen),&
6356
- single_quote// end_array) ! ']
6358
+
6359
+ double_quotes = path(istart+1 :istart+1 ) == quotation_mark ! ["
6360
+
6361
+ if (double_quotes .or. path(istart+1 :istart+1 )==single_quote) then ! ['
6362
+
6363
+ ! it might be a key value: ['abc']
6364
+
6365
+ istart = istart + 1 ! move counter to ' index
6366
+ if (double_quotes) then
6367
+ iend = istart + index (path(istart+1 :ilen),&
6368
+ quotation_mark// end_array) ! "]
6369
+ else
6370
+ iend = istart + index (path(istart+1 :ilen),&
6371
+ single_quote// end_array) ! ']
6372
+ end if
6357
6373
if (iend> istart) then
6374
+
6375
+ ! istart iend
6376
+ ! | |
6377
+ ! ['abcdefg']
6378
+
6358
6379
if (iend> istart+1 ) then
6359
6380
token = path(istart+1 :iend-1 )
6360
6381
else
@@ -6376,15 +6397,18 @@ subroutine json_get_by_path_jsonpath_bracket(json,me,path,p,found,create_it,was_
6376
6397
' " in path: ' // trim (path))
6377
6398
exit
6378
6399
end if
6379
- iend = iend + 1 ! move to ]
6400
+ iend = iend + 1 ! move counter to ] index
6380
6401
else
6381
6402
call json% throw_exception(&
6382
6403
' Error in json_get_by_path_jsonpath_bracket: ' // &
6383
6404
' invalid path: ' // trim (path))
6384
6405
exit
6385
6406
end if
6407
+
6386
6408
else
6387
- ! it might be an integer value
6409
+
6410
+ ! it might be an integer value: [123]
6411
+
6388
6412
iend = istart + index (path(istart+1 :ilen),end_array) ! ]
6389
6413
if (iend> istart+1 ) then
6390
6414
@@ -6434,7 +6458,9 @@ subroutine json_get_by_path_jsonpath_bracket(json,me,path,p,found,create_it,was_
6434
6458
' invalid path: ' // trim (path))
6435
6459
exit
6436
6460
end if
6461
+
6437
6462
end if
6463
+
6438
6464
else
6439
6465
call json% throw_exception(&
6440
6466
' Error in json_get_by_path_jsonpath_bracket: ' // &
0 commit comments