From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: GNATCOLL JSON Parsing Date: Mon, 03 Dec 2018 18:10:35 +0000 Organization: A noiseless patient Spider Message-ID: References: <9524b3ee-476f-4af6-ab83-b15a6c2a417c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="45525c44073b606483464d806cadf990"; logging-data="30002"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/DQQzPsO5OEfTaOuG+oDfUmR/6bFyvVGs=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (darwin) Cancel-Lock: sha1:H01Ppj9RvWOA1SpP+nyUKvdrXgQ= sha1:3ElcaZMyfu4VS2bVWtyksHtBGts= Xref: reader01.eternal-september.org comp.lang.ada:54928 Date: 2018-12-03T18:10:35+00:00 List-Id: eduardsapotski@gmail.com writes: > Again problem ... > I'm trying parse such result: https://api.exmo.com/v1/pair_settings/ > > function Get_Pair_Settings return Pair_Settings_Vector_Package.Vector is > JSON : Unbounded_String := Null_Unbounded_String; > URL_Str : Unbounded_String := To_Unbounded_String("https://api.exmo.com/v1/pair_settings/"); > > Current_JSON_Item, JSON_Main_Node : JSON_Value := Create; > JSON_Result_Array : JSON_Array := Empty_Array; > > Result : Pair_Settings_Vector_Package.Vector; > begin > JSON := To_Unbounded_String(Message_Body(Get(URL => To_String(URL_Str)))); > JSON_Main_Node := Read(JSON); > JSON_Result_Array := Get(JSON_Main_Node); > > if Length(JSON_Result_Array) > 0 then > for i in 1 .. Length(JSON_Result_Array) loop > Put_Line("123"); > end loop; > end if; > > return Result; > > end; > > ... > > raised CONSTRAINT_ERROR : gnatcoll-json.adb:1328 discriminant check failed I finally managed to make this work. It turns out that the JSON returned from that query (actually, the http: version) is a single JSON object, where the contents are fields, not array elements (I may have the nomenclature wrong there). This code with Aws; with Aws.Client; with Aws.Response; with Ada.Text_IO; use Ada.Text_IO; with GNATCOLL.JSON; use GNATCOLL.JSON; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; procedure Test_JSON is Aws_Reply : Aws.Response.Data; JSON_Main_Node : JSON_Value := Create; procedure Report_Outer_Field_Name (Name : Utf8_String; Value : JSON_Value); procedure Report_Inner_Field_Name (Name : Utf8_String; Value : JSON_Value); procedure Report_Outer_Field_Name (Name : Utf8_String; Value : JSON_Value) is begin Put_Line ("field: " & Name); Map_JSON_Object (Value, Report_Inner_Field_Name'Access); end Report_Outer_Field_Name; procedure Report_Inner_Field_Name (Name : Utf8_String; Value : JSON_Value) is begin Put_Line (" field: " & Name & ", kind: " & Kind (Value)'Image); end Report_Inner_Field_Name; begin Aws_Reply := Aws.Client.Get (Url => "http://api.exmo.com/v1/pair_settings"); JSON_Main_Node := Read (Strm => Unbounded_String'(Aws.Response.Message_Body(Aws_Reply)), Filename => ""); Map_JSON_Object (JSON_Main_Node, Report_Outer_Field_Name'Access); end Test_JSON; results in $ ./test_json field: BTC_USD field: min_quantity, kind: JSON_STRING_TYPE field: max_quantity, kind: JSON_STRING_TYPE field: min_price, kind: JSON_STRING_TYPE field: max_price, kind: JSON_STRING_TYPE field: max_amount, kind: JSON_STRING_TYPE field: min_amount, kind: JSON_STRING_TYPE field: BTC_EUR field: min_quantity, kind: JSON_STRING_TYPE field: max_quantity, kind: JSON_STRING_TYPE field: min_price, kind: JSON_STRING_TYPE field: max_price, kind: JSON_STRING_TYPE field: max_amount, kind: JSON_STRING_TYPE field: min_amount, kind: JSON_STRING_TYPE ... field: BTG_ETH field: min_quantity, kind: JSON_STRING_TYPE field: max_quantity, kind: JSON_STRING_TYPE field: min_price, kind: JSON_STRING_TYPE field: max_price, kind: JSON_STRING_TYPE field: max_amount, kind: JSON_STRING_TYPE field: min_amount, kind: JSON_STRING_TYPE Struggles: As noted several times, this was all a bit mystifying The default build of GNATCOLL has no debug symbols (both static & shared libraries)