0

Sorry if my English is bad and write comment if you don't understand (I will edit post). I have script that gets JSON and parses it with order (How I can parse json with arrays by custom order?). It works well for one JSON. My problem is that I need to parse several JSON (like in script 3 JSON) at the same time to get correct order of md5's.

I tried connect JSON files to get one JSON for parsing, but it is not connecting. Script:

var jsonForparse =[]
  for (page=1;page<3;page++) {

url = "https://some.url/to/json"+page+".json";
xhr.open('GET', url, false);
xhr.send();
json=xhr.responseText ;
//Parse json string
json=JSON.parse(json);
//Connect jsons to jsonForparse
     js =json.concat(js)

  }
//Parse jsonForparse
md5= ids.map(id => jsonForparse.posts.find(post => post.id === id))

How should I parse json's or connect json's to parse them to get correct order of md5's Example: Should get:

md5=[12,34,56,78,90,100]

Order:

ids=[2227726,2277,2218681,22881,6659,2236659]

Json1:


{
   "posts":[
      {
         "id":2236659,

         "file":{

            "size":1325351,
            "md5":"100"
         }
      },
      {
         "id":2227726,

         "file":{

            "size":1182791,
            "md5":"12"
         }
      },
      {
         "id":2218681,

         "file":{
"size":1241188,
"md5":"56"
         }
      }
   ]
}

Json2:

{
   "posts":[
      {
         "id":6659,

         "file":{

            "size":1325351,
            "md5":"90"
         }
      },
      {
         "id":2277,

         "file":{

            "size":1182791,
            "md5":"34"
         }
      },
      {
         "id":22881,

         "file":{
"size":1241188,
"md5":"78"
         }
      }
   ]
}

2 Answers 2

1

You need to parse each JSON into object as you are doing for one now and than to join them, after which you have to sort them:

const obj1 = JSON.parse(json1)
const obj2 = JSON.parse(json2)
... \\ this could be a loop if you have more
const obj = [...obj1.posts, ...obj2.posts].sort((a,b) => a.file.md5 - b.file.md5)
const ids = obj.map(e => e.id)
const md5s = obj.map(e => e.file.md5)
Sign up to request clarification or add additional context in comments.

2 Comments

I used obj=[...obj1.posts, ...obj2.posts] with some improvement. I connected it with that then parsed normally with md5= ids.map(id => obj.find(post => post.id === id))
@Tofi if I understand you want to sort by IDs and have whole objects available, than maybe simpler solution (if IDs are numbers) would be: ''' obj = [...obj1.posts, ...obj2.posts].sort((a,b) => a.file.id- b.file.id) '''
1

Embrace the asynchronous nature of JS :p, performs you http calls asynchronously on the first line. Then wait for them to be ready with Promise.all , and finally merge and sort them.

const p = [1, 2, 3].map(idx => fetch(`https://some.url/to/json${idx}.json`).then(res => res.json()));

Promise.all(p).then(results => {
    let posts = results.reduce((acc, val) => acc = [...acc, ...val.posts], [])
    let sorted = ids.map(id => posts.find(post => post.id === id))
    // do smthg here
});

6 Comments

I need order via list of id. Like in end of myscript.
so in sort change .sort((a, b) => a.file.md5 - b.file.md5) by this .sort((a, b) => a.id - b.id) edited my response aswell
i fact its not clear what you want seems you're orderning by md5 but you said the contrary, btw with the provided code you can get by.
Please look last line of script. There script order json md5 via list. If ids=[2236659,2227726,2277,2218681,22881,6659]then md5=[100,12,34,56,78,90]. It demands of Order in example, script read order of id then change of ordering whoal json.
If that not possible then how I can parse each json like another answer. When I parse each json it give me thislist1=[,,,5364,46545,7646];list2=[2846,56645,864854,,,] how I can connect lists with replacing them of there place like this list=[2846,56645,864854,5364,46545,7646]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.