Score:1

Post data to term field through JSONAPI

de flag

Trying to create node via JsonAPi. It contains a term field and text field. Managed to post text field without any issues but struggle to post values to term field. Anyone guide me to post values to term fields. format shared below

"data": {
    "type": "node--verification",
    "attributes": {
        "title": "My custom title"

       }
    "relationships": {
        "field_executing_agency": {
            "data":"BRO",
            "target_id": "1324"
        }

    }
}
Score:0
cn flag

In JSON:API, taxonomy term fields are handled like other entity reference fields.

Posting the data below will set the entity reference field field_entity_reference_taxonomy_term to an existing taxonomy term.

"data": {
    "type": "node--node-type",
    "attributes": {
        "title": "My custom title"
       }
    "relationships": {
        "field_entity_reference_taxonomy_term": {
            "type":"taxonomy-term-type-machine-name",
            "id": "taxonomy-term-uuid"
        }

    }
}

If you want to create a new taxonomy term and set the value of field_entity_reference_taxonomy_term to that, then you have two steps:

  1. POST the new taxonomy term.
  2. Update the node to set the value returned from the server when you POSTed the taxonomy term (the server returns the UUID in the response when it creates a new entity/term).

On a standard installation of Drupal, there's a nice UI for automatically creating taxonomy terms on the node edit page, but with decoupled Drupal, you have to build this yourself.

de flag
Thanks, Patrick. can you please tell me whether this field_entity_reference_taxonomy_term means field name and how to get that term type machine name
cn flag
The field name under relationships is the machine name of the entity reference field. One easy way to find the name is to go to Structure / Content Types / My content type / Manage Fields and then click the entity reference field. The field machine name will be in the URL. An even easier way to get the name is to install the JSONAPI Extras or JSONAPI Explorer modules, but then you have another module. Explorer let’s you see the structure, and Extras let’s you rename stuff and disable unneeded items.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.