• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit Search
0

Script to sort a JSON string

Participant ,
Dec 22, 2022 Dec 22, 2022

Copy link to clipboard

Copied

I need a script to sort a JSON string by name similar to the sample provided below.

var data = {
"Tom":{
"id": "20114",
"age": "21",
"gender": "men"
},
"John":{
"id": "28957",
"age": "20",
"gender": "men"
},
"Sara":{
"id": "8957",
"age": "24",
"gender": "women"
},
"Rose":{
"id": "2178",
"age": "22",
"gender": "women"
}
}

Thank you ahead of time.

TOPICS
How to , JavaScript , PDF forms

Views

166

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2022 Dec 22, 2022

Copy link to clipboard

Copied

what do you mean by sort?  Object members do not have an ordering that can be controlled. 

Do you want the ordering so that the elements will appear alphabetically in a list or dropdown?  If so then it would be better to sort the items when added to the list.

 

For example

 

var aNameList = [];
for(var nm in data )
    aNameList.push(nm);

// Now sort the list
aNameList.sort();

// then add to dropdown
this.getField("mydropdown").setItems(aNameList);

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 22, 2022 Dec 22, 2022

Copy link to clipboard

Copied

Thom,

 

That's what I have been doing (sorting the items when added to the list), however I was under the assumption that if I used JSON.parse on a JSON string I could then sort the data after adding another name and its properties in which case the multidimensional array has now been presorted by name so when converted back to a string, it no longer requires sorting when updating my drop down list. Just thought I could get rid of sorting the temporary array that loads and updates the drop down list. Does that make sense even though the stored data as a string really doesn't have to be in alphabetical order since the drop list always will be the way I've been doing it? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 22, 2022 Dec 22, 2022

Copy link to clipboard

Copied

So in brief, the sample JSON string I provided in my initial post cannot be sorted alphabetically by name once I use JSON. parse? Once again, I realize I can extract the names to create a tempoary array and sort by name to use to replace/update my drop down list. I was just under the impression this could also be achieved by using JSON.parse and presorting the objet array by name but if I understand you correctly, the object cannot be sorted in this manner.  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 23, 2022 Dec 23, 2022

Copy link to clipboard

Copied

LATEST

A JSON string cannot be sorted on its key names and left as JSON. Well, it can but when the JSON is read, there is no order to it. You need to sort the JSON as you use it, or convert it to another format. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines