CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Inconsistency in returned json for chained get and chained create
Pages: [1]

Author Topic: Inconsistency in returned json for chained get and chained create  (Read 690 times)

johanv

  • I post occasionally
  • **
  • Posts: 65
  • Karma: 5
  • #chiro #geek #linux #beer
    • my homepage
  • CiviCRM version: 4.7.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.x
  • PHP version: 5.x
Inconsistency in returned json for chained get and chained create
January 12, 2015, 05:46:03 am
Hi all,

I noticed something strange today, about the JSON that API calls return.

Suppose I want to get a contact and its websites, using chaining. This is the request I use:

Code: [Select]
/dev1/sites/all/modules/civicrm/extern/rest.php?entity=Contact&action=get&json={"sequential":1,"id":9076,"api.Website.get":{}}&api_key=yoursitekey&key=yourkey
Here is the result:

Code: [Select]
    {
    "is_error":0,
    "version":3,
    "count":1,
    "id":9076,
    "values":[{
    "contact_id":"9076",
    "contact_type":"Individual",
    // ...
    "api.Website.get":{
    "is_error":0,
    "version":3,
    "count":2,
    "values":[{
    "id":"59",
    "contact_id":"9076",
    "url":"http:\/\/example.com"
    },
    {
    "id":"60",
    "contact_id":"9076",
    "url":"http:\/\/example.org"
    }]
    }
    }]
    }

Now if I create a new contact, and I provide websites using a chained create action like this:

Code: [Select]
/dev1/sites/all/modules/civicrm/extern/rest.php?entity=Contact&action=create&json={"sequential":1,"last_name":"Smith","contact_type":"Individual","api.Website.create":[{"url":"http://smith.org"},{"url":"http://smath.org"}]}&api_key=yoursitekey&key=yourkey
This is the result I get:

Code: [Select]
    {
    "is_error":0,
    "version":3,
    "count":1,
    "id":9077,
    "values":{
    "0":{
    "id":"9077",
    "contact_type":"Individual",
// ...
    "9077":{
    "api.Website.create":[{
    "is_error":0,
    "version":3,
    "count":1,
    "id":61,
    "values":[{
    "id":"61",
    "contact_id":"9077",
    "url":"http:\/\/smith.org",
    "website_type_id":""
    }]
    },
    {
    "is_error":0,
    "version":3,
    "count":1,
    "id":62,
    "values":[{
    "id":"62",
    "contact_id":"9077",
    "url":"http:\/\/smath.org",
    "website_type_id":""
    }]
    }]
    }
    }
    }

So there are some extra indices ("0") and Id's ("9077"), which makes the parsing of the json harder (for me at least ;-)). It appears to me that 'sequential=1' is ignored.

Is this expected behaviour, or is this a bug? I would like to get a result like this:

Code: [Select]
    {
    "is_error":0,
    "version":3,
    "count":1,
    "id":9077,
    "values":[{
    "id":"9077",
      "contact_type":"Individual",
// ...
  "api.Website.create":[{
"is_error":0,
  "version":3,
  "count":1,
  "id":61,
  "values":[{
  "id":"61",
"contact_id":"9077",
"url":"http:\/\/smith.org",
"website_type_id":""
}]
},
{
"is_error":0,
"version":3,
"count":1,
"id":62,
"values":[{
"id":"62",
"contact_id":"9077",
"url":"http:\/\/smath.org",
"website_type_id":""
}]
}]
    }]
    }

Or even better:

Code: [Select]
    {
    "is_error":0,
    "version":3,
    "count":1,
    "id":9077,
    "values":[{
    "id":"9077",
    "contact_type":"Individual",
// ...
"api.Website.create":{
"is_error":0,
"version":3,
"count":2,
"id":61,
"values":[{
"id":"61",
"contact_id":"9077",
"url":"http:\/\/smith.org",
"website_type_id":""
},
                                {
"id":"62",
"contact_id":"9077",
"url":"http:\/\/smath.org",
"website_type_id":""
}]
}
    }]
    }

Is such a thing doable by adding some API option or something?

Thank you for your advice,
Johan
« Last Edit: January 12, 2015, 12:52:32 pm by johanv »

johanv

  • I post occasionally
  • **
  • Posts: 65
  • Karma: 5
  • #chiro #geek #linux #beer
    • my homepage
  • CiviCRM version: 4.7.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.x
  • PHP version: 5.x
Re: Inconsistency in returned json get and create
January 12, 2015, 06:26:22 am
Some extra info:

If I create only one website, instead of two, I get the desired result.

Request:

Code: [Select]
/dev1/sites/all/modules/civicrm/extern/rest.php?entity=Contact&action=create&json={"sequential":1,"last_name":"Smithers","api.Website.create":{"url":"http://smith.org"},"contact_type":"Individual"}&api_key=yoursitekey&key=yourkey
Reply:

Code: [Select]
    {
    "is_error":0,
    "version":3,
    "count":1,
    "id":9082,
    "values":[{
    "id":"9082",
    "contact_type":"Individual",
    // ...
    "api.Website.create":{
    "is_error":0,
    "version":3,
    "count":1,
    "id":72,
    "values":[{
    "id":"72",
    "contact_id":"9082",
    "url":"http:\/\/smith.org",
    "website_type_id":""
    }]
    }
    }]
    }
« Last Edit: January 12, 2015, 06:30:16 am by johanv »

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Inconsistency in returned json for chained get and chained create
January 12, 2015, 08:37:33 am
Would be great if you could submit a PR or patch to fix this.
Try asking your question on the new CiviCRM help site.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Inconsistency in returned json for chained get and chained create
January 12, 2015, 02:03:33 pm
So, you are saying that when chaining the top level 'sequential' => 1 is overwritten - which sounds 'likely' as I expect the indice is used in calling the nested function. But presumably could be improved (with a patch). I would say you could log it as a bug in JIRA but unless you provide a unit test or a patch / PR it's likely to be treated as low priority
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Inconsistency in returned json for chained get and chained create
January 12, 2015, 02:31:51 pm
I'm not sure it's the case for your real code, but you should have most of the params as post, not get (especially the site and api keys). if you use api/class.api.php (that you can copy in your code) it should provide you a wrapper that does the needed.

As for your issue, can you try to put sequential=1 as a post or get param instead of inside of the json param, see if it behave better?

-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

johanv

  • I post occasionally
  • **
  • Posts: 65
  • Karma: 5
  • #chiro #geek #linux #beer
    • my homepage
  • CiviCRM version: 4.7.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.x
  • PHP version: 5.x
Re: Inconsistency in returned json for chained get and chained create
January 29, 2015, 12:18:31 pm
Quote from: xavier on January 12, 2015, 02:31:51 pm
As for your issue, can you try to put sequential=1 as a post or get param instead of inside of the json param, see if it behave better?

I tried this, but it didn't help.

johanv

  • I post occasionally
  • **
  • Posts: 65
  • Karma: 5
  • #chiro #geek #linux #beer
    • my homepage
  • CiviCRM version: 4.7.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.x
  • PHP version: 5.x
Re: Inconsistency in returned json for chained get and chained create
January 30, 2015, 08:36:57 am
I created an issue for this:
https://issues.civicrm.org/jira/browse/CRM-15891

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Inconsistency in returned json for chained get and chained create

This forum was archived on 2017-11-26.