How do I use JSON.generator to generate an unnamed array?How do I generate a random string?Scheduled Apex Class Testing IssueJson Generator Object within ArraySystem.JSONException: Unexpected character ('}' (code 125))How to deserialize JSON to sObject?JSONError - System.JSONException: Can not start an object, expecting field nameAPEX Deserializing/Parsing RESTful JSON Webservice ResponseHow to grab everything between start of substring and and of text?Apex Quick Start — How do I use the Book?JSON generation in apex batch - how to correctly configure cc in email sending

Did WWII Japanese soldiers engage in cannibalism of their enemies?

Is The Lion King live action film made in motion capture?

How to say "fit" in Latin?

Team goes to lunch frequently, I do intermittent fasting but still want to socialize

Ex-contractor published company source code and secrets online

Could one become a successful researcher by writing some really good papers while being outside academia?

Can I call myself an assistant professor without a PhD

Word or idiom defining something barely functional

Why are the inside diameters of some pipe larger than the stated size?

Should I self-publish my novella on Amazon or try my luck getting publishers?

Is there a way to create a report for the failed entries while calling REST API

How to remove something from the slug/url

Acceptable to cut steak before searing?

How would I as a DM create a smart phone-like spell/device my players could use?

Do other countries guarantee freedoms that the United States does not have?

Can you use Shapechange with character feats to Grapple the Tarrasque?

Plausibility of Ice Eaters in the Arctic

Does two puncture wounds mean venomous snake?

Replace data between quotes in a file

Are there any financial disadvantages to living significantly "below your means"?

What word can be used to describe a bug in a movie?

Keeping a Weakness Secret

How quickly could a country build a tall concrete wall around a city?

Colleagues speaking another language and it impacts work



How do I use JSON.generator to generate an unnamed array?


How do I generate a random string?Scheduled Apex Class Testing IssueJson Generator Object within ArraySystem.JSONException: Unexpected character ('}' (code 125))How to deserialize JSON to sObject?JSONError - System.JSONException: Can not start an object, expecting field nameAPEX Deserializing/Parsing RESTful JSON Webservice ResponseHow to grab everything between start of substring and and of text?Apex Quick Start — How do I use the Book?JSON generation in apex batch - how to correctly configure cc in email sending






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I'm trying to generate:




"["hello" : "world"]"




gen.writeStartObject();
gen.writeArrayStart();
gen.writeStartObject();
gen.writeStringFeild('hello', 'world');
gen.writeEndObject();
gen.writeArrayEnd();
gen.writeEndObject();


Throws the error:
System.JSONException: Can not start an array, expecting field name



But the array in my example does not have a field name?










share|improve this question






























    2















    I'm trying to generate:




    "["hello" : "world"]"




    gen.writeStartObject();
    gen.writeArrayStart();
    gen.writeStartObject();
    gen.writeStringFeild('hello', 'world');
    gen.writeEndObject();
    gen.writeArrayEnd();
    gen.writeEndObject();


    Throws the error:
    System.JSONException: Can not start an array, expecting field name



    But the array in my example does not have a field name?










    share|improve this question


























      2












      2








      2








      I'm trying to generate:




      "["hello" : "world"]"




      gen.writeStartObject();
      gen.writeArrayStart();
      gen.writeStartObject();
      gen.writeStringFeild('hello', 'world');
      gen.writeEndObject();
      gen.writeArrayEnd();
      gen.writeEndObject();


      Throws the error:
      System.JSONException: Can not start an array, expecting field name



      But the array in my example does not have a field name?










      share|improve this question














      I'm trying to generate:




      "["hello" : "world"]"




      gen.writeStartObject();
      gen.writeArrayStart();
      gen.writeStartObject();
      gen.writeStringFeild('hello', 'world');
      gen.writeEndObject();
      gen.writeArrayEnd();
      gen.writeEndObject();


      Throws the error:
      System.JSONException: Can not start an array, expecting field name



      But the array in my example does not have a field name?







      apex






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 29 at 16:40









      muchaviemuchavie

      763 bronze badges




      763 bronze badges























          1 Answer
          1






          active

          oldest

          votes


















          5














          Right now, you open an object before opening your array. An object must consist of field value pairs. Here is an annotated version of what you wrote above, with your three typos corrected.



          gen.writeStartObject();
          // output:
          // remove this line

          gen.writeStartArray();
          // output: [
          // your post had a typo here which I have fixed
          // using writeStartArray rather than writeArrayStart

          gen.writeStartObject();
          // output:

          gen.writeStringField('hello', 'world');
          // output: "hello": "world"
          // your post had a typo here which I have fixed
          // using writeStringField rather than writeStringFeild

          gen.writeEndObject();
          // output:

          gen.writeEndArray();
          // output: ]
          // your post had a typo here which I have fixed
          // using writeEndArray rather than writeArrayEnd

          gen.writeEndObject();
          // output:
          // remove this line


          Please note that JSONGenerator is almost never the correct choice for the job. It is almost universally simpler, easier to read, and better performance to just use out of the box serialization/deserialization. For example, here you could write just one line:



          String payload = JSON.serialize(new List<Object> 
          new Map<String, String> 'hello' => 'world'
          );





          share|improve this answer






















          • 1





            Also its writeStartArray and writeEndArray

            – salesforce-sas
            Jul 29 at 17:00











          • Thank you Adrian!

            – muchavie
            Jul 29 at 17:35






          • 1





            @muchavie If this answer addressed your problem, please consider accepting it by clicking on the check mark/tick to the left of the answer, turning it green. This marks the question as resolved to your satisfaction, and awards reputation both to you and the person who answered. If you have >= 15 reputation points, you may also upvote the answer if you wish. There is no obligation to do either.

            – Adrian Larson
            Jul 29 at 17:38











          • I already upvoted it. I don't know why it didn't turn green.

            – muchavie
            Jul 31 at 13:53











          • Upvoting and accepting are completely different mechanisms. You have to click the little checkmark next to the answer to indicate that it has solved your problem.

            – Adrian Larson
            Jul 31 at 13:56













          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "459"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f271469%2fhow-do-i-use-json-generator-to-generate-an-unnamed-array%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          Right now, you open an object before opening your array. An object must consist of field value pairs. Here is an annotated version of what you wrote above, with your three typos corrected.



          gen.writeStartObject();
          // output:
          // remove this line

          gen.writeStartArray();
          // output: [
          // your post had a typo here which I have fixed
          // using writeStartArray rather than writeArrayStart

          gen.writeStartObject();
          // output:

          gen.writeStringField('hello', 'world');
          // output: "hello": "world"
          // your post had a typo here which I have fixed
          // using writeStringField rather than writeStringFeild

          gen.writeEndObject();
          // output:

          gen.writeEndArray();
          // output: ]
          // your post had a typo here which I have fixed
          // using writeEndArray rather than writeArrayEnd

          gen.writeEndObject();
          // output:
          // remove this line


          Please note that JSONGenerator is almost never the correct choice for the job. It is almost universally simpler, easier to read, and better performance to just use out of the box serialization/deserialization. For example, here you could write just one line:



          String payload = JSON.serialize(new List<Object> 
          new Map<String, String> 'hello' => 'world'
          );





          share|improve this answer






















          • 1





            Also its writeStartArray and writeEndArray

            – salesforce-sas
            Jul 29 at 17:00











          • Thank you Adrian!

            – muchavie
            Jul 29 at 17:35






          • 1





            @muchavie If this answer addressed your problem, please consider accepting it by clicking on the check mark/tick to the left of the answer, turning it green. This marks the question as resolved to your satisfaction, and awards reputation both to you and the person who answered. If you have >= 15 reputation points, you may also upvote the answer if you wish. There is no obligation to do either.

            – Adrian Larson
            Jul 29 at 17:38











          • I already upvoted it. I don't know why it didn't turn green.

            – muchavie
            Jul 31 at 13:53











          • Upvoting and accepting are completely different mechanisms. You have to click the little checkmark next to the answer to indicate that it has solved your problem.

            – Adrian Larson
            Jul 31 at 13:56















          5














          Right now, you open an object before opening your array. An object must consist of field value pairs. Here is an annotated version of what you wrote above, with your three typos corrected.



          gen.writeStartObject();
          // output:
          // remove this line

          gen.writeStartArray();
          // output: [
          // your post had a typo here which I have fixed
          // using writeStartArray rather than writeArrayStart

          gen.writeStartObject();
          // output:

          gen.writeStringField('hello', 'world');
          // output: "hello": "world"
          // your post had a typo here which I have fixed
          // using writeStringField rather than writeStringFeild

          gen.writeEndObject();
          // output:

          gen.writeEndArray();
          // output: ]
          // your post had a typo here which I have fixed
          // using writeEndArray rather than writeArrayEnd

          gen.writeEndObject();
          // output:
          // remove this line


          Please note that JSONGenerator is almost never the correct choice for the job. It is almost universally simpler, easier to read, and better performance to just use out of the box serialization/deserialization. For example, here you could write just one line:



          String payload = JSON.serialize(new List<Object> 
          new Map<String, String> 'hello' => 'world'
          );





          share|improve this answer






















          • 1





            Also its writeStartArray and writeEndArray

            – salesforce-sas
            Jul 29 at 17:00











          • Thank you Adrian!

            – muchavie
            Jul 29 at 17:35






          • 1





            @muchavie If this answer addressed your problem, please consider accepting it by clicking on the check mark/tick to the left of the answer, turning it green. This marks the question as resolved to your satisfaction, and awards reputation both to you and the person who answered. If you have >= 15 reputation points, you may also upvote the answer if you wish. There is no obligation to do either.

            – Adrian Larson
            Jul 29 at 17:38











          • I already upvoted it. I don't know why it didn't turn green.

            – muchavie
            Jul 31 at 13:53











          • Upvoting and accepting are completely different mechanisms. You have to click the little checkmark next to the answer to indicate that it has solved your problem.

            – Adrian Larson
            Jul 31 at 13:56













          5












          5








          5







          Right now, you open an object before opening your array. An object must consist of field value pairs. Here is an annotated version of what you wrote above, with your three typos corrected.



          gen.writeStartObject();
          // output:
          // remove this line

          gen.writeStartArray();
          // output: [
          // your post had a typo here which I have fixed
          // using writeStartArray rather than writeArrayStart

          gen.writeStartObject();
          // output:

          gen.writeStringField('hello', 'world');
          // output: "hello": "world"
          // your post had a typo here which I have fixed
          // using writeStringField rather than writeStringFeild

          gen.writeEndObject();
          // output:

          gen.writeEndArray();
          // output: ]
          // your post had a typo here which I have fixed
          // using writeEndArray rather than writeArrayEnd

          gen.writeEndObject();
          // output:
          // remove this line


          Please note that JSONGenerator is almost never the correct choice for the job. It is almost universally simpler, easier to read, and better performance to just use out of the box serialization/deserialization. For example, here you could write just one line:



          String payload = JSON.serialize(new List<Object> 
          new Map<String, String> 'hello' => 'world'
          );





          share|improve this answer















          Right now, you open an object before opening your array. An object must consist of field value pairs. Here is an annotated version of what you wrote above, with your three typos corrected.



          gen.writeStartObject();
          // output:
          // remove this line

          gen.writeStartArray();
          // output: [
          // your post had a typo here which I have fixed
          // using writeStartArray rather than writeArrayStart

          gen.writeStartObject();
          // output:

          gen.writeStringField('hello', 'world');
          // output: "hello": "world"
          // your post had a typo here which I have fixed
          // using writeStringField rather than writeStringFeild

          gen.writeEndObject();
          // output:

          gen.writeEndArray();
          // output: ]
          // your post had a typo here which I have fixed
          // using writeEndArray rather than writeArrayEnd

          gen.writeEndObject();
          // output:
          // remove this line


          Please note that JSONGenerator is almost never the correct choice for the job. It is almost universally simpler, easier to read, and better performance to just use out of the box serialization/deserialization. For example, here you could write just one line:



          String payload = JSON.serialize(new List<Object> 
          new Map<String, String> 'hello' => 'world'
          );






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 29 at 17:32

























          answered Jul 29 at 16:44









          Adrian LarsonAdrian Larson

          115k19 gold badges135 silver badges274 bronze badges




          115k19 gold badges135 silver badges274 bronze badges










          • 1





            Also its writeStartArray and writeEndArray

            – salesforce-sas
            Jul 29 at 17:00











          • Thank you Adrian!

            – muchavie
            Jul 29 at 17:35






          • 1





            @muchavie If this answer addressed your problem, please consider accepting it by clicking on the check mark/tick to the left of the answer, turning it green. This marks the question as resolved to your satisfaction, and awards reputation both to you and the person who answered. If you have >= 15 reputation points, you may also upvote the answer if you wish. There is no obligation to do either.

            – Adrian Larson
            Jul 29 at 17:38











          • I already upvoted it. I don't know why it didn't turn green.

            – muchavie
            Jul 31 at 13:53











          • Upvoting and accepting are completely different mechanisms. You have to click the little checkmark next to the answer to indicate that it has solved your problem.

            – Adrian Larson
            Jul 31 at 13:56












          • 1





            Also its writeStartArray and writeEndArray

            – salesforce-sas
            Jul 29 at 17:00











          • Thank you Adrian!

            – muchavie
            Jul 29 at 17:35






          • 1





            @muchavie If this answer addressed your problem, please consider accepting it by clicking on the check mark/tick to the left of the answer, turning it green. This marks the question as resolved to your satisfaction, and awards reputation both to you and the person who answered. If you have >= 15 reputation points, you may also upvote the answer if you wish. There is no obligation to do either.

            – Adrian Larson
            Jul 29 at 17:38











          • I already upvoted it. I don't know why it didn't turn green.

            – muchavie
            Jul 31 at 13:53











          • Upvoting and accepting are completely different mechanisms. You have to click the little checkmark next to the answer to indicate that it has solved your problem.

            – Adrian Larson
            Jul 31 at 13:56







          1




          1





          Also its writeStartArray and writeEndArray

          – salesforce-sas
          Jul 29 at 17:00





          Also its writeStartArray and writeEndArray

          – salesforce-sas
          Jul 29 at 17:00













          Thank you Adrian!

          – muchavie
          Jul 29 at 17:35





          Thank you Adrian!

          – muchavie
          Jul 29 at 17:35




          1




          1





          @muchavie If this answer addressed your problem, please consider accepting it by clicking on the check mark/tick to the left of the answer, turning it green. This marks the question as resolved to your satisfaction, and awards reputation both to you and the person who answered. If you have >= 15 reputation points, you may also upvote the answer if you wish. There is no obligation to do either.

          – Adrian Larson
          Jul 29 at 17:38





          @muchavie If this answer addressed your problem, please consider accepting it by clicking on the check mark/tick to the left of the answer, turning it green. This marks the question as resolved to your satisfaction, and awards reputation both to you and the person who answered. If you have >= 15 reputation points, you may also upvote the answer if you wish. There is no obligation to do either.

          – Adrian Larson
          Jul 29 at 17:38













          I already upvoted it. I don't know why it didn't turn green.

          – muchavie
          Jul 31 at 13:53





          I already upvoted it. I don't know why it didn't turn green.

          – muchavie
          Jul 31 at 13:53













          Upvoting and accepting are completely different mechanisms. You have to click the little checkmark next to the answer to indicate that it has solved your problem.

          – Adrian Larson
          Jul 31 at 13:56





          Upvoting and accepting are completely different mechanisms. You have to click the little checkmark next to the answer to indicate that it has solved your problem.

          – Adrian Larson
          Jul 31 at 13:56

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Salesforce Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f271469%2fhow-do-i-use-json-generator-to-generate-an-unnamed-array%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form