search

Home  >  Q&A  >  body text

Join JSON string

Is there a special way to concatenate each individual string block with | like this

9000234|Test NPC|0|0|0|0|0|0|0|0| Does not include isEnabled. I also don't want other blocks to merge with each other. I'm not quite sure what to do.

"100_npc": {
        "uniqueId": 9000234,
        "username": "Test NPC",
        "itemsObj": {"colour": 0, "head": 0, "face": 0, "body": 0, "neck": 0, "hand": 0, "feet": 0, "flag": 0, "photo": 0},
        "isEnabled": true
    },
    "101_npc": {
        "uniqueId": 9000251,
        "username": "Pelican",
        "itemsObj": {"colour": 0, "head": 0, "face": 0, "body": 0, "neck": 0, "hand": 0, "feet": 0, "flag": 0, "photo": 0},
        "isEnabled": true
    }
}

P粉035600555P粉035600555230 days ago480

reply all(2)I'll reply

  • P粉194919082

    P粉1949190822024-04-02 14:15:02

    I only want to search for specific attribute names, such as "npc_100"

    const data =  {  
      "100_npc": {"uniqueId": 9000234,"username": "Test NPC","itemsObj": {"colour": 0, "head": 0, "face": 0, "body": 0, "neck": 0, "hand": 0, "feet": 0, "flag": 0, "photo": 0},"isEnabled": true},
      "101_npc": {"uniqueId": 9000251,"username": "Pelican","itemsObj": {"colour": 0, "head": 0, "face": 0, "body": 0, "neck": 0, "hand": 0, "feet": 0, "flag": 0, "photo": 0},"isEnabled": true}
    };
    
    
    const prop = "100_npc";
    
    const result = Object.entries(data[prop]).reduce((a, [k, v]) => 
      (k !== "isEnabled" && a.push(k==="itemsObj" ? Object.values(v) : v), a)
    , []).flat().join("|");
    
    console.log(result)

    For files Output:

    const data =  {  
      "100_npc": {"uniqueId": 9000234,"username": "Test NPC","itemsObj": {"colour": 0, "head": 0, "face": 0, "body": 0, "neck": 0, "hand": 0, "feet": 0, "flag": 0, "photo": 0},"isEnabled": true},
      "101_npc": {"uniqueId": 9000251,"username": "Pelican","itemsObj": {"colour": 0, "head": 0, "face": 0, "body": 0, "neck": 0, "hand": 0, "feet": 0, "flag": 0, "photo": 0},"isEnabled": true}
    };
    
    const result = Object.values(data).map(entry => Object.entries(entry).reduce((a, [k, v]) => 
      (k !== "isEnabled" && a.push(k==="itemsObj" ? Object.values(v) : v), a)
    , []).flat().join("|")).join("\n");
    
    console.log(result)

    reply
    0
  • P粉579008412

    P粉5790084122024-04-02 09:00:18

    This seems to output what you asked for: (playground)

        let data = {
          '100_npc': {
            uniqueId: 9000234,
            username: 'Test NPC',
            itemsObj: {
              colour: 0,
              head: 0,
              face: 0,
              body: 0,
              neck: 0,
              hand: 0,
              feet: 0,
              flag: 0,
              photo: 0,
            },
            isEnabled: true,
          },
          '101_npc': {
            uniqueId: 9000251,
            username: 'Pelican',
            itemsObj: {
              colour: 0,
              head: 0,
              face: 0,
              body: 0,
              neck: 0,
              hand: 0,
              feet: 0,
              flag: 0,
              photo: 0,
            },
            isEnabled: true,
          },
        };
        
        let result = [];
        
        for (let key in data) {
          if (data.hasOwnProperty(key)) {
            let item = data[key];
            let arr = [item.uniqueId, item.username];
            for (let objKey in item.itemsObj) {
              arr.push(item.itemsObj[objKey]);
            }
            result.push(arr.join('|'));
          }
        }
        
        console.log(result.join('\n'));

    reply
    0
  • Cancelreply