Parsing JSON

  1. 5 years ago

    I'm sending a HTTPRequest to a wordpress form through its REST api.

    In postman, the resulting JSON looks like this:

    {
        "jtzbo": {
            "id": "3",
            "item_key": "jtzbo",
            "name": "Jean",
            "ip": "",
            "meta": {
                "firstname": "Jean",
                "lastname": "Jacques",
                "email": "schaggi@shopmail.ch",
                "message": "test west bestös"
            },
            "form_id": "7",
            "post_id": "0",
            "user_id": "1",
            "parent_item_id": "0",
            "is_draft": "0",
            "updated_by": "1",
            "created_at": "2018-06-10 13:08:25",
            "updated_at": "2018-06-13 15:54:37"
        },
        "m5kyw": {
            "id": "4",
            "item_key": "m5kyw",
            "name": "Hans",
            "ip": "",
            "meta": {
                "firstname": "Hans",
                "lastname": "Dampf",
                "email": "testowesto@shopmail.ch",
                "message": "Hello! It's me!"
            },
            "form_id": "7",
            "post_id": "0",
            "user_id": "1",
            "parent_item_id": "0",
            "is_draft": "0",
            "updated_by": "1",
            "created_at": "2018-07-28 14:13:00",
            "updated_at": "2018-07-28 14:13:00"
        }
    }

    In my Creo test project, I receive a value in the HTTPClient1.Request.NewValue event, which looks like this:

    [m5kyw:[ip:,name:Hans,created_at:2018-07-28 14:13:00,form_id:7,user_id:1,parent_item_id:0,id:4,item_key:m5kyw,updated_at:2018-07-28 14:13:00,updated_by:1,post_id:0,is_draft:0,meta:[firstname:Hans,email:testowesto@shopmail.ch,message:Hello! It's me!,lastname:Dampf]],jtzbo:[ip:,name:Jean,created_at:2018-06-10 13:08:25,form_id:7,user_id:1,parent_item_id:0,id:3,item_key:jtzbo,updated_at:2018-06-13 15:54:37,updated_by:1,post_id:0,is_draft:0,meta:[firstname:Jean,email:schaggi@shopmail.ch,message:test west bestös,lastname:Jacques]]]

    At design time the IDs of the returned records are not known (aka: "jtzbo" or "m5kyw")

    What I want to do ist to display firstname and lastname in a TableView and store all other information. When the user selects an entry in the TableView, then a detail view should open with all remaining infos visible.

    How do i proceed in Creo/Gravity, to get this done?

  2. Edited 5 years ago by oliver@osswald.com

    Ok, when I process the value, received in the HTTPClient1.Request.NewValue event like below, then I'm getting my hands on the values which are in the subitem with the key "meta".

    However, this code is quite clumsy and I guess one can do this shorter and in a more elegant way?

    Any hints for improvement are welcome:

    for (var id in value.keys()) {
    	// Console.write("Key : " + id);
    	
    	var record = value["\(id)"];
    	// Console.write("\(record)");
    
    	var i = 0;
    	var t = record.count;
    	var fields = record.keys();
    
    	while (i < t) {
    		var f = fields[i];
    
    		if (f == "meta") {
    		 	var person = record["meta"];
    			var firstname = person["firstname"];
    			var lastname  = person["lastname"];
    			var email     = person["email"];
    			var message   = person["message"];
    
    		 	Console.write("\(firstname) \(lastname) \(email) \(message)");
    		} 
    		i += 1;
    	}
    }

    -image-

  3. Now I would like to add firstname and lastname to TableView1? How is this done?

    I also would like to create a class with properties like "firstname", "lastname" etc, then instantiate it and fill in the results from the above retrieved data. Then I would like to store such object with each row in the TableView1, if possible (like a RowTag).

    The idea is, that when a user selects a row in the TableView1, then I would like to display the detail-data in a new view.

    How would one do this in Creo?

  4. Joel_Eisenstat

    29 Jul 2018 User since 2016

    Hi Oliver,
    Create SQLite db to map your json data.
    Push your data to the db
    Use db query to pop the TableView
    Manipulate TableView by looping rows and/or computed property
    Use identifier ( id) of row to create single row query in detail view/window.
    HTH
    Joel

  5. @Joel_Eisenstat : Thanks Joel, and yes - I think this will work.

  6. andrea

    30 Jul 2018 Administrator User since 2016

    @oliver@osswald.com ,
    at the moment there is not a better way than the one suggested by Joel to use this kind of data directly in a TableView. A TableView only looks for arrays of objects in the decoded JSON value and this JSON has a Map (dictionary) instead.
    We are working to add a simpler way to use this kind of data.

or Sign Up to reply!