# Import custom fields of user with import\_scripts Ruby

**URL:** https://meta.discourse.org/t/import-custom-fields-of-user-with-import-scripts-ruby/275683
**Category:** Migration
**Tags:** drupal
**Created:** [August 18, 2023, 10:17am UTC](https://meta.discourse.org/t/import-custom-fields-of-user-with-import-scripts-ruby/275683 "2023-08-18T10:17:32Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![LN\_test](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ln_test/32/321469_2.png) [@LN\_test](https://meta.discourse.org/u/LN_test)
#### Post date: [August 18, 2023, 10:17am UTC](https://meta.discourse.org/t/import-custom-fields-of-user-with-import-scripts-ruby/275683/1 "2023-08-18T10:17:32Z")

</div>

> <https://github.com/discourse/discourse/blob/main/script/import_scripts/drupal_json.rb>

 ![image](https://global.discourse-cdn.com/meta/original/4X/d/2/4/d24cfe6179c128fb463fc931f764d9e205bfde5e.png)  
I’ve created the **DrupalID** field from user fields and now I want to import data into this

```plaintext
create_users(@users_json) do |u|
      {
# id: u["uid"],
# name: u["name"],
# email: u["mail"],#
# custom_fields: {
# '1':u["uid"],
# 'DrupalID':u["uid"]
# },

        id: 2037,
        name: "Test 7",
        email: "test7@gmail.com",
        custom_fields: {
          '1':'test',
          'DrupalID':"test"
        },

        created_at: Time.zone.at(u["created"].to_i),
      }

```

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [August 18, 2023, 12:02pm UTC](https://meta.discourse.org/t/import-custom-fields-of-user-with-import-scripts-ruby/275683/2 "2023-08-18T12:02:58Z")

</div>

Is this a question is an answer? Are you saying that your code works?

---

<div class="post-metadata">

### Author: ![LN\_test](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ln_test/32/321469_2.png) [@LN\_test](https://meta.discourse.org/u/LN_test)
#### Post date: [August 18, 2023, 12:23pm UTC](https://meta.discourse.org/t/import-custom-fields-of-user-with-import-scripts-ruby/275683/3 "2023-08-18T12:23:40Z")

</div>

Yes code works but not for custom fields  
I want to update custom fields also

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [August 18, 2023, 12:38pm UTC](https://meta.discourse.org/t/import-custom-fields-of-user-with-import-scripts-ruby/275683/4 "2023-08-18T12:38:17Z")

</div>

Search the other importer scripts for “custom\_field”. I’m sure there are examples; I’m fairly certain I’ve done it before.

---

<div class="post-metadata">

### Author: ![LN\_test](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ln_test/32/321469_2.png) [@LN\_test](https://meta.discourse.org/u/LN_test)
#### Post date: [August 18, 2023, 12:42pm UTC](https://meta.discourse.org/t/import-custom-fields-of-user-with-import-scripts-ruby/275683/5 "2023-08-18T12:42:01Z")

</div>

Yes I checked but we haven’t any example provided of JSON file and field name (what we needs to use)

---

<div class="post-metadata">

### Author: ![LN\_test](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ln_test/32/321469_2.png) [@LN\_test](https://meta.discourse.org/u/LN_test)
#### Post date: [August 18, 2023, 12:43pm UTC](https://meta.discourse.org/t/import-custom-fields-of-user-with-import-scripts-ruby/275683/6 "2023-08-18T12:43:06Z")

</div>

Also i try to import with CSV, but I have same case (Example files missing)

> <https://github.com/discourse/discourse/blob/main/script/import_scripts/csv_importer.rb#L136C8-L136C8>

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [August 18, 2023, 1:18pm UTC](https://meta.discourse.org/t/import-custom-fields-of-user-with-import-scripts-ruby/275683/7 "2023-08-18T13:18:37Z")

</div>

Just find any script that imports custom user fields and add that to your script

---

<div class="post-metadata">

### Author: ![LN\_test](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ln_test/32/321469_2.png) [@LN\_test](https://meta.discourse.org/u/LN_test)
#### Post date: [August 18, 2023, 1:39pm UTC](https://meta.discourse.org/t/import-custom-fields-of-user-with-import-scripts-ruby/275683/8 "2023-08-18T13:39:52Z")

</div>

I tried also custom\_fields.csv from [discourse/script/import\_scripts/csv\_importer.rb at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/main/script/import_scripts/csv_importer.rb)  
Field name **DrupalID**

```plaintext
id,user_field_1,DrupalID,Drupal ID,custom_field_DrupalID,drupalid
23456,John,John,Doe,example,johndoe
23457,Jane,Jane,Smith,example1,janesmith

```

 ![image](https://global.discourse-cdn.com/meta/original/4X/d/2/4/d24cfe6179c128fb463fc931f764d9e205bfde5e.png)

It’s not working, showing field blank

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [August 18, 2023, 2:09pm UTC](https://meta.discourse.org/t/import-custom-fields-of-user-with-import-scripts-ruby/275683/9 "2023-08-18T14:09:18Z")

</div>

Looks like [discourse/script/import\_scripts/csv\_importer.rb at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/main/script/import_scripts/csv_importer.rb#L69-L78) should work, but you have to define the ENV seen in [discourse/script/import\_scripts/csv\_importer.rb at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/main/script/import_scripts/csv_importer.rb#L11).
