Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REGARDING JSON READ PROBLEM

Status
Not open for further replies.

JINESH GANDHI

IS-IT--Management
May 8, 2022
12
0
0
IN
Hello Team,

I have a problem regarding json read

Json String
[{"id":0,"title":"Ungrouped","items":[{"id":1613,"alarm":0,"name":"GJ.01.DV.6890","online":"online","time":"29-10-2023 17:39:43","timestamp":1698581386,"acktimestamp":1698578510,"lat":21.775102,"lng":70.632152,"course":47,"speed":42,"altitude":107,"icon_type":"arrow","icon_color":"green","icon_colors":{"moving":"green","stopped":"red","offline":"black","engine":"yellow"},"icon":{"id":0,"user_id":null,"type":"arrow","order":1,"width":25,"height":33,"path":"assets\/images\/arrow-ack.png","by_status":0},"power":"-","address":"-","protocol":"-","driver":"Driver ABC","driver_data":{"id":135,"user_id":73,"device_id":1613,"device_port":null,"name":"Driver ABC","rfid":"1234566789","phone":"9898998989","email":"abc@gmail.com","description":"Licence No: 213456","created_at":"2022-08-29 10:20:49","updated_at":"2022-08-29 10:23:37"},"sensors":[{"id":12146,"type":"ignition","name":"Ignition","show_in_popup":0,"value":"Off","val":false,"scale_value":null},{"id":12147,"type":"battery","name":"Battery","show_in_popup":0,"value":"94 %","val":"94","scale_value":5},{"id":12148,"type":"tachometer","name":"Power","show_in_popup":0,"value":"28 V","val":"27.666","scale_value":null},{"id":12149,"type":"satellites","name":"Satellite","show_in_popup":0,"value":"15","val":"15","scale_value":null},{"id":12150,"type":"fuel_tank_calibration","name":"Fuel Level (Diesel)","show_in_popup":0,"value":"357 Ltr","val":356.88,"scale_value":null},{"id":12151,"type":"numerical","name":"Tank Capacity","show_in_popup":0,"value":"416 Ltrs","val":"416","scale_value":null},{"id":12152,"type":"odometer","name":"Odometer","show_in_popup":0,"value":"99155 Kms","val":99154.956,"scale_value":null},{"id":12153,"type":"gsm","name":"GSM Signal","show_in_popup":0,"value":"100 %","val":100,"scale_value":5},{"id":12154,"type":"temperature","name":"Temperature","show_in_popup":0,"value":"39 \u00b0C","val":"39","scale_value":null}],"services":[],"tail":[],"distance_unit_hour":"kph","unit_of_distance":"km","unit_of_altitude":"mt","unit_of_capacity":"lt","stop_duration":"12s","stop_duration_sec":12,"moved_timestamp":1698581383,"engine_status":null,"detect_engine":"gps","engine_hours":"gps","total_distance":220366.25,"inaccuracy":null,"sim_expiration_date":null,"device_data":{"id":1613,"user_id":76,"current_driver_id":135,"timezone_id":null,"traccar_device_id":1614,"icon_id":0,"icon_colors":{"moving":"green","stopped":"red","offline":"black","engine":"yellow"},"active":0,"kind":0,"deleted":0,"name":"GJ.01.DV.6890","imei":"350544501723517","fuel_measurement_id":1,"fuel_quantity":"0.00","fuel_price":"0.00","fuel_per_km":"0.0000","fuel_per_h":"0.0000","sim_number":null,"msisdn":null,"device_model":"FMB125","plate_number":"GJ.01.DV.6890","vin":"","registration_number":"VI","object_owner":"Kataria Enterprise","additional_notes":"Old IMEI 354018113575645","authentication":null,"comment":"","expiration_date":null,"sim_expiration_date":null,"sim_activation_date":null,"installation_date":null,"tail_color":"#33cc33","tail_length":0,"engine_hours":"gps","detect_engine":"gps","detect_speed":"gps","detect_distance":null,"min_moving_speed":6,"min_fuel_fillings":10,"min_fuel_thefts":10,"snap_to_road":0,"gprs_templates_only":0,"valid_by_avg_speed":0,"parameters":"[\"priority\",\"sat\",\"event\",\"ignition\",\"motion\",\"rssi\",\"io69\",\"di1\",\"io202\",\"io204\",\"io113\",\"io252\",\"power\",\"io24\",\"battery\",\"adc1\",\"io201\",\"io203\",\"io16\",\"io11\",\"io14\",\"combinedfuel\",\"sequence\",\"distance\",\"totaldistance\",\"valid\",\"enginehours\",\"out1\",\"io205\",\"io206\",\"operator\",\"workmode\",\"io200\",\"io68\",\"io199\",\"di4\",\"result\",\"gpsstatus\"]","currents":null,"created_at":"2021-07-22 10:04:09","updated_at":"2023-04-15 06:49:10","forward":null,"device_type_id":null,"app_tracker_login":0,"fuel_detect_sec_after_stop":null,"users":[{"id":76,"email":"plant.api@katariaenterprise.com"}],"pivot":{"user_id":76,"device_id":1613,"group_id":0,"current_driver_id":135,"active":0,"timezone_id":null},"icon":{"id":0,"user_id":null,"type":"arrow","order":1,"width":25,"height":33,"path":"assets\/images\/arrow-ack.png","by_status":0},"traccar":{"id":1614,"name":"GJ.01.DV.6890","uniqueId":"350544501723517","latestPosition_id":2346628,"lastValidLatitude":21.7751016,"lastValidLongitude":70.6321516,"other":"<info><priority>0<\/priority><sat>15<\/sat><event>0<\/event><ignition>true<\/ignition><motion>true<\/motion><rssi>5<\/rssi><io69>1<\/io69><di1>0<\/di1><io202>39<\/io202><io204>0<\/io204><io113>94<\/io113><power>27.666<\/power><io24>41<\/io24><battery>4.062<\/battery><adc1>0<\/adc1><io201>3315<\/io201><io203>0<\/io203><io16>99154956<\/io16><io11>8991052220<\/io11><io14>514947194<\/io14><combinedfuel>1657<\/combinedfuel><sequence>39<\/sequence><distance>170.47<\/distance><totaldistance>220366253.89<\/totaldistance><valid>true<\/valid><enginehours>22214460<\/enginehours><\/info>","speed":"42.00","time":"2023-10-29 12:09:43","device_time":"2023-10-29 12:09:43","server_time":"2023-10-29 12:09:46","ack_time":"2023-10-29 11:21:50","altitude":107,"course":47,"power":null,"address":null,"protocol":"teltonika","latest_positions":"21.7751016\/70.6321516;21.7741533\/70.6308516;21.7734816\/70.62936;21.7730949\/70.6278366;21.7727366\/70.6263566;21.7724266\/70.6247733;21.7721133\/70.6232483;21.7717849\/70.6217433;21.7718\/70.6197916;21.7718666\/70.61791;21.77189\/70.6161233;21.7717333\/70.6142983;21.7709116\/70.6128899;21.7697299\/70.6116266;21.7686216\/70.6104583","moved_at":"2023-10-29 12:09:43","stoped_at":"2023-10-29 11:17:56","move_begin_at":"2023-10-29 11:18:11","stop_begin_at":"2023-10-29 11:17:56","parked_end_at":"2023-10-29 11:19:56","engine_on_at":"2023-10-29 12:09:43","engine_off_at":"2023-10-29 10:03:14","engine_changed_at":"2023-10-29 10:03:19","updated_at":"2023-10-29 12:09:46","database_id":null},"sensors":[{"id":12146,"user_id":117,"device_id":1613,"name":"Ignition","type":"ignition","tag_name":"di1","add_to_history":0,"on_value":null,"off_value":null,"shown_value_by":null,"fuel_tank_name":null,"full_tank":null,"full_tank_value":null,"min_value":null,"max_value":null,"formula":null,"odometer_value_by":null,"odometer_value":null,"odometer_value_unit":"km","temperature_max":null,"temperature_max_value":null,"temperature_min":null,"temperature_min_value":null,"value":"0","value_formula":0,"show_in_popup":0,"unit_of_measurement":"","on_tag_value":"1","off_tag_value":"0","on_type":1,"off_type":1,"calibrations":null,"skip_calibration":null,"skip_empty":null,"decbin":null,"hexbin":null},{"id":12147,"user_id":117,"device_id":1613,"name":"Battery","type":"battery","tag_name":"io113","add_to_history":0,"on_value":null,"off_value":null,"shown_value_by":"tag_value","fuel_tank_name":null,"full_tank":null,"full_tank_value":null,"min_value":null,"max_value":null,"formula":null,"odometer_value_by":null,"odometer_value":null,"odometer_value_unit":"km","temperature_max":null,"temperature_max_value":null,"temperature_min":null,"temperature_min_value":null,"value":"94","value_formula":0,"show_in_popup":0,"unit_of_measurement":"%","on_tag_value":null,"off_tag_value":null,"on_type":null,"off_type":null,"calibrations":null,"skip_calibration":null,"skip_empty":0,"decbin":0,"hexbin":0},{"id":12148,"user_id":117,"device_id":1613,"name":"Power","type":"tachometer","tag_name":"power","add_to_history":0,"on_value":null,"off_value":null,"shown_value_by":null,"fuel_tank_name":null,"full_tank":null,"full_tank_value":null,"min_value":null,"max_value":null,"formula":"[value]","odometer_value_by":null,"odometer_value":null,"odometer_value_unit":"km","temperature_max":null,"temperature_max_value":null,"temperature_min":null,"temperature_min_value":null,"value":"27.666","value_formula":0,"show_in_popup":0,"unit_of_measurement":"V","on_tag_value":null,"off_tag_value":null,"on_type":null,"off_type":null,"calibrations":null,"skip_calibration":null,"skip_empty":null,"decbin":null,"hexbin":null},{"id":12149,"user_id":117,"device_id":1613,"name":"Satellite","type":"satellites","tag_name":"sat","add_to_history":0,"on_value":null,"off_value":null,"shown_value_by":null,"fuel_tank_name":null,"full_tank":null,"full_tank_value":null,"min_value":null,"max_value":null,"formula":null,"odometer_value_by":null,"odometer_value":null,"odometer_value_unit":"km","temperature_max":null,"temperature_max_value":null,"temperature_min":null,"temperature_min_value":null,"value":"-","value_formula":0,"show_in_popup":0,"unit_of_measurement":"","on_tag_value":null,"off_tag_value":null,"on_type":null,"off_type":null,"calibrations":null,"skip_calibration":null,"skip_empty":null,"decbin":null,"hexbin":null},{"id":12150,"user_id":117,"device_id":1613,"name":"Fuel Level","type":"fuel_tank_calibration","tag_name":"io201","add_to_history":0,"on_value":null,"off_value":null,"shown_value_by":null,"fuel_tank_name":"Diesel","full_tank":null,"full_tank_value":null,"min_value":null,"max_value":null,"formula":null,"odometer_value_by":null,"odometer_value":null,"odometer_value_unit":"km","temperature_max":null,"temperature_max_value":null,"temperature_min":null,"temperature_min_value":null,"value":"356.88","value_formula":0,"show_in_popup":0,"unit_of_measurement":"Ltr","on_tag_value":null,"off_tag_value":null,"on_type":null,"off_type":null,"calibrations":{"0":"0","65532":"00","65":"30","164":"40","270":"50","366":"60","469":"70","574":"80","678":"90","784":"100","882":"110","985":"120","1089":"130","1189":"140","1291":"150","1390":"160","1491":"170","1590":"180","1691":"190","1789":"200","1889":"210","1987":"220","2085":"230","2185":"240","2281":"250","2379":"260","2477":"270","2574":"280","2671":"290","2767":"300","2864":"310","2960":"320","3056":"330","3153":"340","3249":"350","3345":"360","3434":"370","3532":"380","3632":"390","3735":"400","3843":"410","3901":"415","4095":"416"},"skip_calibration":0,"skip_empty":null,"decbin":0,"hexbin":0},{"id":12151,"user_id":117,"device_id":1613,"name":"Tank Capacity","type":"numerical","tag_name":"adc1","add_to_history":0,"on_value":null,"off_value":null,"shown_value_by":null,"fuel_tank_name":null,"full_tank":null,"full_tank_value":null,"min_value":null,"max_value":null,"formula":"416","odometer_value_by":null,"odometer_value":null,"odometer_value_unit":"km","temperature_max":null,"temperature_max_value":null,"temperature_min":null,"temperature_min_value":null,"value":"416","value_formula":0,"show_in_popup":0,"unit_of_measurement":"Ltrs","on_tag_value":null,"off_tag_value":null,"on_type":null,"off_type":null,"calibrations":null,"skip_calibration":null,"skip_empty":0,"decbin":0,"hexbin":0},{"id":12152,"user_id":117,"device_id":1613,"name":"Odometer","type":"odometer","tag_name":"io16","add_to_history":0,"on_value":null,"off_value":null,"shown_value_by":null,"fuel_tank_name":null,"full_tank":null,"full_tank_value":null,"min_value":null,"max_value":null,"formula":"[value]\/1000","odometer_value_by":"connected_odometer","odometer_value":null,"odometer_value_unit":"km","temperature_max":null,"temperature_max_value":null,"temperature_min":null,"temperature_min_value":null,"value":"99154.956","value_formula":99155,"show_in_popup":0,"unit_of_measurement":"Kms","on_tag_value":null,"off_tag_value":null,"on_type":null,"off_type":null,"calibrations":null,"skip_calibration":null,"skip_empty":null,"decbin":null,"hexbin":null},{"id":12153,"user_id":117,"device_id":1613,"name":"GSM Signal","type":"gsm","tag_name":"rssi","add_to_history":0,"on_value":null,"off_value":null,"shown_value_by":null,"fuel_tank_name":null,"full_tank":null,"full_tank_value":null,"min_value":"0","max_value":"5","formula":null,"odometer_value_by":null,"odometer_value":null,"odometer_value_unit":"km","temperature_max":null,"temperature_max_value":null,"temperature_min":null,"temperature_min_value":null,"value":"-","value_formula":0,"show_in_popup":0,"unit_of_measurement":"%","on_tag_value":null,"off_tag_value":null,"on_type":null,"off_type":null,"calibrations":null,"skip_calibration":null,"skip_empty":null,"decbin":null,"hexbin":null},{"id":12154,"user_id":117,"device_id":1613,"name":"Temperature","type":"temperature","tag_name":"io202","add_to_history":0,"on_value":null,"off_value":null,"shown_value_by":null,"fuel_tank_name":null,"full_tank":null,"full_tank_value":null,"min_value":null,"max_value":null,"formula":"[value]","odometer_value_by":null,"odometer_value":null,"odometer_value_unit":"km","temperature_max":null,"temperature_max_value":null,"temperature_min":null,"temperature_min_value":null,"value":"39","value_formula":0,"show_in_popup":0,"unit_of_measurement":"\u00b0C","on_tag_value":null,"off_tag_value":null,"on_type":null,"off_type":null,"calibrations":null,"skip_calibration":null,"skip_empty":null,"decbin":null,"hexbin":null}],"services":[],"driver":{"id":135,"user_id":73,"device_id":1613,"device_port":null,"name":"Driver ABC","rfid":"1234566789","phone":"9898998989","email":"abc@gmail.com","description":"Licence No: 213456","created_at":"2022-08-29 10:20:49","updated_at":"2022-08-29 10:23:37"},"lastValidLatitude":21.775102,"lastValidLongitude":70.632152,"latest_positions":"21.7751016\/70.6321516;21.7741533\/70.6308516;21.7734816\/70.62936;21.7730949\/70.6278366;21.7727366\/70.6263566;21.7724266\/70.6247733;21.7721133\/70.6232483;21.7717849\/70.6217433;21.7718\/70.6197916;21.7718666\/70.61791;21.77189\/70.6161233;21.7717333\/70.6142983;21.7709116\/70.6128899;21.7697299\/70.6116266;21.7686216\/70.6104583","icon_type":"arrow","enable":1,"group_id":0,"user_timezone_id":null,"time":"2023-10-29 12:09:43","course":47,"speed":42}}]}]


My Code (Note Json start with Squar ([) Braket)
CLOSE DATABASES
CLEAR ALL
USE jj1
Set Procedure To nfJsonRead additive
result=ALLTRIM(c1)
loJson = nfjsonread(result)
mval=loJson.items(1).id
?loJson.id

But found error
Members Item does not evaluate to an object.

Guid me
 
You are looking for items array, wich is a member of the object
in the first row of the (unnamed) root array.

Since nfJson always returns an object:

myObject = nfjsonRead('["a","b","c"]')
? myObject.array(1) && = 'a'

use the debugger to inspect your object and drag and drop any property name
to the command line or editor to get the full path.

For example, the highlighted path in below image is
? lojson.array[1].items[1].device_data.created_at


2023-10-29_10_26_40_wjhm9a.png



vfp color theme using






Marco Plaza
@nfoxProject
 
JSON notation basics

Something in square brackets represents an array
Something in curly brackets represents an object.

If you give nfjsonreadd.prg a JSON string that represents an array, the major structure of the returned loJSON object is not loJSON having an items collection, but it is having an array property instead:

loJSON.array.array[1] then is the first array element.

Chriss
 
Sorry, this overlapped. I see Marco already answered.

Still here's one more tip: If you have a json string, just put it into _cliptext (copy it to the clipboard) and paste it into the Javascript console of a browser, the answer to that is a treeview representation of what the JSON string represents.

Here's a screenshot of that in comparison to the treeview the VFP debugger shows when you look into the loJSON result of nfjsonread.

jsonparseresult_f04plq.jpg


I won't go into details, just notice how this structure (aside from visual differences in the representation) is the same.

The goal of nfjson is to emulate what a JS engine would make of that json, not to give you a result object that always has the result in loJSON.items.item[1]. Marco can't replicate what JSON does 1:1, because of differences you can't replicate in VFP, but almost 1:1, and that also means depending on what the root level of the json is you either get loJSON.items or loJSON.array, maybe also other things.

The array in the JSON string has no name, the object within the array has no name. VFP can't have nameless items. Even the browser console representation of the objects within the array gives the nameless objects collection the name "items", so that's even replicating what a JS engine does.

Well, what's the essence of all this? You surely got loJSON.items.item[1] from other JSON and thought, this is always what to take as the actual result. Did you ever wonder why Marco didn't simply put item[1] into loJSON itself and get rid of this unnecessarily complex structure? Marco is just doing what JS is doing in the best way you can represent it in VFP. So you have to have at least a bit of understanding of what the JSON actually represents to be able to make use of the nfjsonread result.

I'd say it's not Marcos code that overcomplicates things, it's you oversimplifying. It's a bit like expecting XMLTOCURSOR to always work and turn XML into a VFP table. It depends on what the XML is, whether it can be turned into a table and it depends on what the JSON string represents, what structure the loSON result has in detail.

Make use of the debugger after your first parsing and you know what to expect in general, when the JSON you parse comes from an API that in general returns the same type of result. That way you kjnow what exactly to code to get at the root object or array of the JSON you need to process.

But just like XML JSON isn't always the same and there is no one size fits all solution. Go into the details, and make use of the tools at hand. Use the debugger. Use a browser, and extend your knowledge, you're missing some basics.

Chriss
 
Thank You All for Cooperation to solve the problem.

Solution for name less array
CLOSE DATABASES
CLEAR ALL
USE jj1
Set Procedure To nfJsonRead additive
result=ALLTRIM(c1)
loJson = nfjsonread(result)
mval=loJson.array(1).items(1).id

For name less json use object.array


Thank you all again.
 
JINESH GANDHI said:
For name less json use object.array

No, take a closer look.

The nameless array itself is in loJSON.array, and elements of it are in loJason.array.array[n].

Whether the next part of the expression is item or not depends on whether the array is an array of objects or an array of arrays or whatever else.

So you don't get around of analyzing the JSON structure that's coming back for individual API calls. An API call that is meant to return the same
type of data structure will also allow you to write code that works for all results of that call, but you can't have standard code just depending
on whether the first bracket is squaree or curly.

That's, again, oversimplyfying things.

What I said and what I repeat here, different API and every individual API call/method will return individual JSON for the use case. An API
returning a single data record will always have the same JSON structure, but that could be an array with elements being record fields, that could
be an object with properties. JSON in itself doesn't define a standard how APIs must structure their results.

So no, you can't now write code that either looks into loJSON.items.item(1) or loJSON.array.items(1). You could also get
loJSON.items.item(1).array.array(1) and anything else. With individual API and with each individual call of an API you at first can also look into
the VFP debugger locals window ora browsers JS console and see what you get and how you have to program access to individual data you want to get.
Forget about finding patterns that would work for all APIs

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top